Createroot - React
Createroot - React
v19
createRoot
createRoot lets you create a root to display React components
inside a browser DOM node.
Reference
createRoot(domNode, options?)
root.render(reactNode)
root.unmount()
Usage
Troubleshooting
Reference
createRoot(domNode, options?)
https://react.dev/reference/react-dom/client/createRoot 1/16
20/02/2025, 19:13 createRoot – React
React will create a root for the domNode , and take over managing the DOM
inside it. After you’ve created a root, you need to call root.render to display
a React component inside of it:
root.render(<App />);
An app fully built with React will usually only have one createRoot call for its
root component. A page that uses “sprinkles” of React for parts of the page
may have as many separate roots as needed.
Parameters
domNode : A DOM element. React will create a root for this DOM element
and allow you to call functions on the root, such as render to display
rendered React content.
https://react.dev/reference/react-dom/client/createRoot 2/16
20/02/2025, 19:13 createRoot – React
Returns
Caveats
You’ll likely have only one createRoot call in your app. If you use a
framework, it might do this call for you.
When you want to render a piece of JSX in a different part of the DOM
tree that isn’t a child of your component (for example, a modal or a
tooltip), use createPortal instead of createRoot .
root.render(reactNode)
Call root.render to display a piece of JSX (“React node”) into the React
root’s browser DOM node.
root.render(<App />);
React will display <App /> in the root , and take over managing the DOM
inside it.
https://react.dev/reference/react-dom/client/createRoot 3/16
20/02/2025, 19:13 createRoot – React
Parameters
reactNode : A React node that you want to display. This will usually be a
piece of JSX like <App /> , but you can also pass a React element
constructed with createElement() , a string, a number, null , or
undefined .
Returns
Caveats
The first time you call root.render , React will clear all the existing HTML
content inside the React root before rendering the React component into
it.
If your root’s DOM node contains HTML generated by React on the server
or during the build, use hydrateRoot() instead, which attaches the event
handlers to the existing HTML.
If you call render on the same root more than once, React will update the
DOM as necessary to reflect the latest JSX you passed. React will decide
which parts of the DOM can be reused and which need to be recreated by
“matching it up” with the previously rendered tree. Calling render on the
same root again is similar to calling the set function on the root
component: React avoids unnecessary DOM updates.
root.unmount()
root.unmount();
An app fully built with React will usually not have any calls to root.unmount .
https://react.dev/reference/react-dom/client/createRoot 4/16
20/02/2025, 19:13 createRoot – React
This is mostly useful if your React root’s DOM node (or any of its ancestors)
may get removed from the DOM by some other code. For example, imagine a
jQuery tab panel that removes inactive tabs from the DOM. If a tab gets
removed, everything inside it (including the React roots inside) would get
removed from the DOM as well. In that case, you need to tell React to “stop”
managing the removed root’s content by calling root.unmount . Otherwise,
the components inside the removed root won’t know to clean up and free up
global resources like subscriptions.
Calling root.unmount will unmount all the components in the root and
“detach” React from the root DOM node, including removing any event
handlers or state in the tree.
Parameters
Returns
Caveats
Calling root.unmount will unmount all the components in the tree and
“detach” React from the root DOM node.
Once you call root.unmount you cannot call root.render again on the
same root. Attempting to call root.render on an unmounted root will
throw a “Cannot update an unmounted root” error. However, you can
create a new root for the same DOM node after the previous root for that
node has been unmounted.
Usage
If your app is fully built with React, create a single root for your entire app.
https://react.dev/reference/react-dom/client/createRoot 5/16
20/02/2025, 19:13 createRoot – React
Usually, you only need to run this code once at startup. It will:
https://react.dev/reference/react-dom/client/createRoot 6/16
20/02/2025, 19:13 createRoot – React
If your app is fully built with React, you shouldn’t need to create any more
roots, or to call root.render again.
From this point on, React will manage the DOM of your entire app. To add
more components, nest them inside the App component. When you need to
update the UI, each of your components can do this by using state. When you
need to display extra content like a modal or a tooltip outside the DOM node,
render it with a portal.
Note
When your HTML is empty, the user sees a blank page until the app’s
JavaScript code loads and runs:
<div id="root"></div>
This can feel very slow! To solve this, you can generate the initial
HTML from your components on the server or during the build. Then
your visitors can read text, see images, and click links before any of
the JavaScript code loads. We recommend using a framework that
does this optimization out of the box. Depending on when it runs,
this is called server-side rendering (SSR) or static site generation
(SSG).
Pitfall
https://react.dev/reference/react-dom/client/createRoot 7/16
20/02/2025, 19:13 createRoot – React
the DOM nodes from your HTML instead of destroying and re-
creating them.
If your page isn’t fully built with React, you can call createRoot multiple
times to create a root for each top-level piece of UI managed by React. You
can display different content in each root by calling root.render .
Here, two different React components are rendered into two DOM nodes
defined in the index.html file:
import './styles.css';
import { createRoot } from 'react-dom/client';
import { Comments, Navigation } from './Components.js';
https://react.dev/reference/react-dom/client/createRoot 8/16
20/02/2025, 19:13 createRoot – React
You could also create a new DOM node with document.createElement() and
add it to the document manually.
To remove the React tree from the DOM node and clean up all the resources
used by it, call root.unmount .
root.unmount();
This is mostly useful if your React components are inside an app written in a
different framework.
You can call render more than once on the same root. As long as the
component tree structure matches up with what was previously rendered,
React will preserve the state. Notice how you can type in the input, which
means that the updates from repeated render calls every second in this
example are not destructive:
https://react.dev/reference/react-dom/client/createRoot 9/16
20/02/2025, 19:13 createRoot – React
let i = 0;
setInterval(() => {
root.render(<App counter={i} />);
i++;
}, 1000);
By default, React will log all errors to the console. To implement your own
error reporting, you can provide the optional error handler root options
onUncaughtError , onCaughtError and onRecoverableError :
https://react.dev/reference/react-dom/client/createRoot 10/16
20/02/2025, 19:13 createRoot – React
Show more
https://react.dev/reference/react-dom/client/createRoot 11/16
20/02/2025, 19:13 createRoot – React
Troubleshooting
Make sure you haven’t forgotten to actually render your app into the root:
https://react.dev/reference/react-dom/client/createRoot 12/16
20/02/2025, 19:13 createRoot – React
Console
This error means that whatever you’re passing to createRoot is not a DOM
node.
at the time of your call. There may be a few reasons for it:
1. The ID you’re looking for might differ from the ID you used in the HTML
file. Check for typos!
https://react.dev/reference/react-dom/client/createRoot 13/16
20/02/2025, 19:13 createRoot – React
2. Your bundle’s <script> tag cannot “see” any DOM nodes that appear
after it in the HTML.
This error means that whatever you’re passing to root.render is not a React
component.
https://react.dev/reference/react-dom/client/createRoot 14/16
20/02/2025, 19:13 createRoot – React
hydrateRoot(
document.getElementById('root'),
<App />
);
Note that its API is different. In particular, usually there will be no further
root.render call.
PREVIOUS
Client APIs
NEXT
hydrateRoot
uwu?
https://react.dev/reference/react-dom/client/createRoot 15/16
20/02/2025, 19:13 createRoot – React
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/client/createRoot 16/16