createRoot
Creates a React root for rendering React components into a DOM container with concurrent rendering features.Reference
createRoot(container, options?)
Call createRoot to create a React root for displaying content inside a browser DOM element.
/src/client/ReactDOMRoot.js:171-260
Parameters
Element | Document | DocumentFragment
required
A DOM element, Document, or DocumentFragment that will contain your React app. React will create a root for this container and manage the DOM inside it.Validation:
- Must be a valid DOM container
- Cannot be a text node or comment node (unless feature flag enabled)
- Should not be already managed by another React root
CreateRootOptions
Optional configuration object for the root.
Returns
RootType
A root object with two methods:
render and unmount.root.render(reactNode)
Renders a React component into the root’s DOM container.
/src/client/ReactDOMRoot.js:107-136
Parameters
ReactNode
required
A React node to render. Usually JSX like
<App />, but can also be a React element created with createElement(), a string, a number, null, or undefined.Notes
- The first call replaces any existing HTML inside the container
- Subsequent calls use React’s diffing algorithm to update efficiently
- Does not support callback argument (removed in React 18)
- Updates are batched and may be asynchronous
root.unmount()
Destroys a rendered React component and cleans up its DOM and event handlers.
/src/client/ReactDOMRoot.js:139-169
Notes
- Runs cleanup functions in
useEffectanduseLayoutEffect - Synchronously completes unmounting before returning
- Should not be called while React is already rendering
- Container is emptied but not removed from the DOM
Usage Examples
Basic Client Rendering
With Error Handling
Multiple Roots on Same Page
Updating the Root
Cleaning Up
TypeScript
Migration from React 17
Before (React 17)
After (React 18+)
Common Pitfalls
Don’t call render in render
Don’t create multiple roots for same container
Don’t forget to handle errors
Browser Compatibility
createRoot requires:
- Modern browsers (Chrome 90+, Firefox 88+, Safari 14.1+, Edge 90+)
- ES6 features: Map, Set, Promise
- DOM features: Element, Document, DocumentFragment
Performance Considerations
- Concurrent rendering - createRoot enables concurrent features like transitions and Suspense
- Automatic batching - Multiple state updates are batched automatically
- Interruptible rendering - React can pause and resume rendering work
- Smooth updates - Long-running updates don’t block user interactions
Related APIs
- hydrateRoot - For hydrating server-rendered content
- flushSync - Force synchronous updates
- createPortal - Render children into different DOM node