Mount an application
Client-rendered single-page applications
React 19+ · Function
createRoot creates a React root connected to a browser DOM element. Call the returned root.render to display React content, or root.unmount to remove the rendered tree.
Import createRoot from react-dom/client and pass it the browser DOM element that React should manage. The returned root exposes render for displaying React nodes and unmount for removing the rendered tree; createRoot itself only establishes the root, so render must be called afterward.
const root = createRoot(domNode, options?)| Name | Type | Description |
|---|---|---|
domNode | Element | The browser DOM element that React will manage and use as the root container. |
options | object | Optional configuration for the root. |
A React root object with render and unmount methods.
import { createRoot } from 'react-dom/client';
function App() {
return <h1>Hello, React!</h1>;
}
const domNode = document.getElementById('root');
if (!domNode) throw new Error('Missing #root element');
const root = createRoot(domNode);
root.render(<App />);Creates one React root for the browser container and renders the application into it.
Client-rendered single-page applications
Embedding React in an existing page
Replacing or updating the content managed by one root
createRoot initializes a React root associated with a browser DOM element. It returns an object whose render method displays React content and whose unmount method destroys the rendered tree.
Follow-up: How should an already server-rendered container be initialized? Use hydrateRoot rather than createRoot so React can hydrate the existing HTML.
Call createRoot with the target DOM node, then call root.render with a React node such as JSX. React then takes over management of the DOM inside that node.
Follow-up: What happens if root.render is omitted? The root exists, but no React component is displayed.
createRoot is for displaying React components in a browser DOM node. Server-rendered applications are not supported with it and should use hydrateRoot instead.
Follow-up: Why is hydration preferable in that case? It reuses the HTML produced by the server rather than replacing it with newly created DOM nodes.
hydrateRootroot.renderroot.unmountcreateElement