React Render HTML
React's job is to put your components on the page. That happens in two steps: you create a root attached to a real DOM element, then you render a React element into that root. From then on React owns everything inside that element.
createRoot and render
The react-dom/client package provides createRoot. Every React app has code like this in its entry file:
import { createRoot } from 'react-dom/client';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<h1>Rendered by React!</h1>);The playground frame contains an empty <div id="root"></div>, just like the index.html of a Vite project. root.render(...) turns the JSX into real DOM nodes inside it.
Note: In most other lessons the playground mounts your
export defaultcomponent automatically, so you don't have to write these lines. Here we do it by hand to see what happens under the hood.
The HTML side
In a real project the HTML file is almost empty. React fills in the rest:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My React App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>The id root is only a convention — any element works, as long as you pass the same element to createRoot.
Rendering a component
Usually you render your top-level component rather than raw markup:
import { createRoot } from 'react-dom/client';
function App() {
const today = new Date().toLocaleDateString('en-GB', { weekday: 'long' });
return (
<div>
<h1>Weekly planner</h1>
<p>Today is {today}.</p>
</div>
);
}
createRoot(document.getElementById('root')).render(<App />);Rendering again updates, not replaces
Calling render a second time on the same root does not rebuild the page. React compares the new element tree with the previous one and changes only what differs. This example renders a clock every second; open your browser's DevTools and you'll see only the text node changing:
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
function tick() {
const time = new Date().toLocaleTimeString();
root.render(
<div>
<h2>Current time</h2>
<p style={{ fontSize: 32, fontFamily: 'monospace' }}>{time}</p>
<input placeholder="Type here — your text survives re-renders" style={{ width: 300 }} />
</div>
);
}
tick();
setInterval(tick, 1000);Type something into the input: it keeps its text even though render runs every second. Because the <input> is at the same position with the same type, React reuses the existing DOM node.
Tip: Real apps almost never call
renderrepeatedly. They render<App />once and let state trigger updates. The clock above is only a demonstration of how React reconciles changes.
Multiple roots
A page can host several independent React roots. This is handy when you add React widgets to an existing, non-React website:
import { createRoot } from 'react-dom/client';
const host = document.getElementById('root');
host.innerHTML = `
<p>This part of the page is plain HTML.</p>
<div id="widget-a"></div>
<p>More plain HTML in between.</p>
<div id="widget-b"></div>
`;
function Badge({ label, color }) {
return (
<span style={{ background: color, color: 'white', padding: '4px 10px', borderRadius: 12 }}>
{label}
</span>
);
}
createRoot(document.getElementById('widget-a')).render(<Badge label="Widget A" color="teal" />);
createRoot(document.getElementById('widget-b')).render(<Badge label="Widget B" color="purple" />);The root API
| Method | Purpose |
|---|---|
createRoot(element) |
Creates a React root that will manage element's content |
root.render(reactNode) |
Shows reactNode inside the root (first time) or updates it |
root.unmount() |
Removes the rendered tree and cleans up |
hydrateRoot(element, node) |
Attaches React to HTML that was already rendered on a server |
Common mistakes
- Rendering into a missing element:
document.getElementById('app')returnsnullif the id doesn't exist, andcreateRoot(null)throws "Target container is not a DOM element". - Calling
createRoottwice on the same element: create the root once and keep callingrenderon it. - Editing React-managed DOM by hand: if you change nodes inside the root with
innerHTML, React's view of the page gets out of sync. Change state instead.
Test yourself
Which package exports createRoot for browser apps?
- react
- react-dom/client
- react/jsx-runtime
- react-router
What happens when you call root.render() again with a slightly different tree?
- The whole container is wiped and rebuilt
- React updates only the parts that changed
- React throws an error
- A second copy is appended
Can one page contain more than one React root?
- No, never
- Yes, each root manages its own DOM element
- Only with class components
- Only in production builds