createRoot
createRoot lets you create a root to display React components inside a browser DOM node.
const root = createRoot(domNode, options?)Reference
createRoot(domNode, options?)
Call createRoot to create a React root for displaying content inside a browser DOM element.
import { createRoot } from 'react-dom/client';
const domNode = document.getElementById('root');
const root = createRoot(domNode);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 asrenderto display rendered React content. -
optional
options: An object with options for this React root.- Canary only optional
onCaughtError: Callback called when React catches an error in an Error Boundary. Called with theerrorcaught by the Error Boundary, and anerrorInfoobject containing thecomponentStack. - Canary only optional
onUncaughtError: Callback called when an error is thrown and not caught by an Error Boundary. Called with theerrorthat was thrown, and anerrorInfoobject containing thecomponentStack. - optional
onRecoverableError: Callback called when React automatically recovers from errors. Called with anerrorReact throws, and anerrorInfoobject containing thecomponentStack. Some recoverable errors may include the original error cause aserror.cause. - optional
identifierPrefix: A string prefix React uses for IDs generated byuseId. Useful to avoid conflicts when using multiple roots on the same page.
- Canary only optional
Returns
createRoot returns an object with two methods: render and unmount.
Caveats
- If your app is server-rendered, using
createRoot()is not supported. UsehydrateRoot()instead. - You’ll likely have only one
createRootcall 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
createPortalinstead ofcreateRoot.
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.
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 withcreateElement(), a string, a number,null, orundefined.
Returns
root.render returns undefined.
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
renderon 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