hydrateRoot
hydrateRoot
lets you display React components inside a browser DOM node whose HTML content was previously generated by react-dom/server
.
const root = hydrateRoot(domNode, reactNode, options?)
Usage
Hydrating server-rendered HTML
If your appâs HTML was generated by react-dom/server
, you need to hydrate it on the client.
import {hydrateRoot} from 'react-dom/client';
hydrateRoot(document.getElementById('root'), <App />);
This will hydrate the server HTML inside the browser DOM node with the React component for your app. Usually, you will do it once at startup. If you use a framework, it might do this behind the scenes for you.
To hydrate your app, React will âattachâ your componentsâ logic to the initial generated HTML from the server. Hydration turns the initial HTML snapshot from the server into a fully interactive app that runs in the browser.
import './styles.css'; import {hydrateRoot} from 'react-dom/client'; import App from './App.js'; hydrateRoot( document.getElementById('root'), <App /> );
You shouldnât need to call hydrateRoot
again or to call it in more places. From this point on, React will be managing the DOM of your application. If you want to update the UI, your components can do this by using state.
Updating a hydrated root component
After the root has finished hydrating, you can call root.render
to update the root React component. Unlike with createRoot
, you donât usually need to do this because the initial content was already rendered as HTML.
If you call root.render
at some point after hydration, and 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:
import {hydrateRoot} from 'react-dom/client'; import './styles.css'; import App from './App.js'; const root = hydrateRoot( document.getElementById('root'), <App counter={0} /> ); let i = 0; setInterval(() => { root.render(<App counter={i} />); i++; }, 1000);
It is uncommon to call root.render
on a hydrated root. Usually, youâll update state inside one of the components instead.
Reference
hydrateRoot(domNode, options?)
Call hydrateRoot
to âattachâ React to existing HTML that was already rendered by React in a server environment.
const domNode = document.getElementById('root');
const root = hydrateRoot(domNode, reactNode);
React will attach to the HTML that exists inside the domNode
, and take over managing the DOM inside it. An app fully built with React will usually only have one hydrateRoot
call with its root component.
Parameters
-
domNode
: A DOM element that was rendered as the root element on the server. -
reactNode
: The âReact nodeâ used to render the existing HTML. This will usually be a piece of JSX like<App />
which was rendered with aReactDOM Server
method such asrenderToPipeableStream(<App />)
. -
optional
options
: A object contain options for this React root.onRecoverableError
: optional callback called when React automatically recovers from errors.identifierPrefix
: optional prefix React uses for IDs generated byuseId
. Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix as used on the server.
Returns
hydrateRoot
returns an object with two methods: render
and unmount
.
Caveats
hydrateRoot()
expects the rendered content to be identical with the server-rendered content. You should treat mismatches as bugs and fix them.- In development mode, React warns about mismatches during hydration. There are no guarantees that attribute differences will be patched up in case of mismatches. This is important for performance reasons because in most apps, mismatches are rare, and so validating all markup would be prohibitively expensive.
- Youâll likely have only one
hydrateRoot
call in your app. If you use a framework, it might do this call for you. - If your app is client-rendered with no HTML rendered already, using
hydrateRoot()
is not supported. UsecreateRoot()
instead.
root.render(reactNode)
Call root.render
to update a React component inside a hydrated React root for a browser DOM element.
root.render(<App />);
React will update <App />
in the hydrated root
.
Parameters
reactNode
: A âReact nodeâ that you want to update. 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
- If you call
root.render
before the root has finished hydrating, React will clear the existing server-rendered HTML content and switch the entire root to client rendering.
root.unmount()
Call root.unmount
to destroy a rendered tree inside a React root.
root.unmount();
An app fully built with React will usually not have any calls to root.unmount
.
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
root.unmount
does not accept any parameters.
Returns
render
returns null
.
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 callroot.render
again on the root. Attempting to callroot.render
on an unmounted root will throw a âCannot update an unmounted rootâ error.