Sub topic 3
Opting in with createRoot
This chapter assumes you can write React function components with useState and props. It also assumes you know that a state update makes React re-render a component tree. You don't need to know how transitions work yet. Later chapters cover them. Here you only need to make sure your app can use them.
Why the root matters
Concurrent rendering is the ability to pause, resume or throw away a render that is in progress. React can't add this to an app that already runs in the old way, because some code relies on every render running to completion without interruption. React 18 therefore ties concurrent features to how you create the root. The root is the object that connects your component tree to a DOM node.
- A root created with
createRootruns in concurrent mode. - A tree rendered with the legacy
ReactDOM.renderruns in legacy mode, which behaves like React 17.
The hooks useTransition and useDeferredValue, and the function startTransition, can all be called in either mode. In a legacy root, though, React renders each update from start to finish without interruption, so they give you none of the responsiveness benefits.
Switching to createRoot
The old entry point looks like this:
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));The new entry point imports from react-dom/client. You create the root once, then call render on it:
import { createRoot } from 'react-dom/client';
import App from './App';
const root = createRoot(document.getElementById('root'));
root.render(<App />);Two details are easy to miss:
- The import path is
react-dom/client, notreact-dom. - The container is passed to
createRoot, not torender. To update the tree later, callroot.renderagain on the same root. Do not create a second root for the same container.
If your app is rendered on the server, replace hydrateRoot for ReactDOM.hydrate. It also lives in react-dom/client:
import { hydrateRoot } from 'react-dom/client';
import App from './App';
hydrateRoot(document.getElementById('root'), <App />);What changes besides transitions
Using createRoot turns on more than transitions. Check these before you ship the change:
- Automatic batching. React groups state updates into one render even when they happen inside promises, timeouts or native event handlers. In legacy mode only React event handlers were batched. Code that relied on a render between two
setStatecalls may behave differently. - Stricter development checks. In development, Strict Mode mounts each component, unmounts it and mounts it again. This exposes effects that don't clean up after themselves.
- Interruptible rendering. A component's render function may run, be discarded, and run again. Keep render functions free of side effects.
How the choice flows
Common errors
- Calling
createRoottwice on one container. React warns you. Keep the root in a variable and reuse it. - Importing
createRootfromreact-dom. It is not exported there. Usereact-dom/client. - Passing the component to
createRoot. The first argument is the DOM element. The component goes torender. - Mixing React 18 with an older
react-dom. Both packages must be on the same major version.
Exercise
After upgrading to React 18, you wrap a slow state update in startTransition, but typing is still laggy and the console warns about ReactDOM.render. What is the likely cause, and what is the fix?
Show answer
The app still renders through the legacy ReactDOM.render, so React renders every update to completion and the transition cannot be interrupted. Replace it with const root = createRoot(document.getElementById('root')); root.render(<App />);, importing createRoot from 'react-dom/client'.