Rendering a React application
Before learning state updates, understand how a React tree gets onto the page.
A basic client entry point looks like this:
import {createRoot} from 'react-dom/client';
import App from './App.jsx';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);
What createRoot does
createRoot creates a React root for a browser DOM node.
#root already exists in the document.That root is the bridge through which React manages the component tree mounted in that DOM container.
What root.render does
root.render asks React to render a React node into that root.
root.render(<App />);
<App /> is a React element description. React evaluates the component tree and coordinates the DOM work needed to display it.
Rendering does not mean "append HTML"
A useful simplified model is:
Later chapters will separate the render phase, commit phase, and browser paint more carefully.
For now, remember:
Rendering is React calculating what the UI should be. DOM mutation happens during the commit work, not every time a component function line is evaluated.
Components form a tree
function App() {
return (
<>
<Header />
<Dashboard />
</>
);
}
If Dashboard renders more components, the tree grows:
React reasons about this tree over time. Position and identity in the tree later become important for state preservation and keys.
A component function describes UI
function Greeting({name}) {
return <h1>Hello, {name}</h1>;
}
Calling the component yourself is not how you normally render it:
// Avoid treating components like ordinary helper calls
Greeting({name: 'Aisha'});
Instead let React own component invocation:
<Greeting name="Aisha" />
This lets React associate Hooks, state, identity, and rendering work with the component correctly.
Initial render vs re-render
The first time a tree is displayed is the initial render.
Later, React can render components again because of updates such as state changes or parent rendering.
root.render introduces the component tree to the root.A re-render does not automatically mean every DOM node is recreated. React calculates the next UI and commits the DOM changes that are needed.
Rendering should be pure
A component should calculate UI from its inputs without changing external systems during render.
Bad:
function Counter({count}) {
document.title = `Count ${count}`;
return <p>{count}</p>;
}
The render function is now mutating an external system.
Better reasoning:
Read current props/state/context and return the UI description. Keep this path pure.
Use an Effect for synchronization or an event handler when the work is caused by a user action.
We will study Effects later. For now, keep render calculation free of side effects.
root.unmount
If another system removes the React application from a page, you can unmount the root:
root.unmount();
This tells React to clean up the mounted component tree and detach from the root DOM node.
Most single-page applications do not manually unmount their only app root during normal use.
createRoot vs hydrateRoot
createRoot is for client rendering into a container that React will manage.
hydrateRoot is used when HTML was already generated on the server and React needs to attach to that existing server-rendered markup.
Start from a container React will populate and manage on the client.
Start from existing server HTML and attach client React to that markup.
Do not use hydration terminology for a normal client-only Vite app.
Common mistakes
Calling createRoot repeatedly for ordinary updates
Create the application root once, then let React updates drive the tree.
Calling component functions manually
Render components with JSX so React controls their lifecycle and Hooks.
Assuming render equals DOM mutation
React may render work that never commits. Keep render pure.
Assuming a parent render always means visible DOM changes
A component can execute again and still produce the same visible result.
Debugging
If nothing appears:
- confirm the root DOM element exists;
- confirm
createRootreceives that element; - confirm
root.renderreceives valid React content; - inspect the browser console for syntax/runtime errors;
- inspect the component tree with React DevTools.
Exercise
Given:
<div id="app"></div>
Write the entry file that renders:
Then explain, in your own words, the difference between:
- the DOM node with id
app; - the React root;
- the
<App />React element; - the
Appcomponent function.
Interview questions
Junior
What is the difference between createRoot and root.render?
Mid-level
Does a component re-render guarantee a DOM update? Why not?
Senior
Why does React require render logic to be pure, especially with interruptible/concurrent rendering?
References
- https://react.dev/reference/react-dom/client/createRoot
- https://react.dev/reference/react-dom/client/hydrateRoot
- https://react.dev/learn/render-and-commit
- https://react.dev/reference/rules/components-and-hooks-must-be-pure
Next
Continue with Strict Mode.