HydrateRoot - React
HydrateRoot - React
hydrateRoot
hydrateRoot lets you display React components inside a browser
DOM node whose HTML content was previously generated by
react-dom/server .
Reference
root.render(reactNode)
root.unmount()
Usage
Reference
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 1/12
28/2/24, 11:32 hydrateRoot – React
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 a
ReactDOM Server method such as renderToPipeableStream(<App />) .
Returns
Caveats
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 2/12
28/2/24, 11:32 hydrateRoot – React
root.render(reactNode)
root.render(<App />);
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 with createElement() , a string, a number, null , or
undefined .
Returns
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.
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 3/12
28/2/24, 11:32 hydrateRoot – React
root.unmount()
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. 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 clean up and free up 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
root. Attempting to call root.render on an unmounted root will throw a
“Cannot update an unmounted root” error.
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 4/12
28/2/24, 11:32 hydrateRoot – React
Usage
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.
1 import './styles.css';
2 import { hydrateRoot } from 'react-dom/client';
3 import App from './App.js';
4
5 hydrateRoot(
6 document.getElementById('root'),
7 <App />
8 );
9
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 5/12
28/2/24, 11:32 hydrateRoot – React
Pitfall
The React tree you pass to hydrateRoot needs to produce the same
output as it did on the server.
This is important for the user experience. The user will spend some
time looking at the server-generated HTML before your JavaScript
code loads. Server rendering creates an illusion that the app loads
faster by showing the HTML snapshot of its output. Suddenly
showing different content breaks that illusion. This is why the server
render output must match the initial render output on the client.
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 6/12
28/2/24, 11:32 hydrateRoot – React
React recovers from some hydration errors, but you must fix them
like other bugs. In the best case, they’ll lead to a slowdown; in the
worst case, event handlers can get attached to the wrong elements.
Apps fully built with React can render the entire document as JSX, including
the <html> tag:
function App() {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1
<link rel="stylesheet" href="/styles.css"></link>
<title>My app</title>
</head>
<body>
<Router />
</body>
</html>
);
}
To hydrate the entire document, pass the document global as the first
argument to hydrateRoot :
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 7/12
28/2/24, 11:32 hydrateRoot – React
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 8/12
28/2/24, 11:32 hydrateRoot – React
This only works one level deep, and is intended to be an escape hatch. Don’t
overuse it. Unless it’s text content, React still won’t attempt to patch it up, so
it may remain inconsistent until future updates.
If you intentionally need to render something different on the server and the
client, you can do a two-pass rendering. Components that render something
different on the client can read a state variable like isClient , which you can
set to true in an Effect:
useEffect(() => {
setIsClient(true);
}, []);
return (
<h1>
{isClient ? 'Is Client' : 'Is Server'}
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 9/12
28/2/24, 11:32 hydrateRoot – React
This way the initial render pass will render the same content as the server,
avoiding mismatches, but an additional pass will happen synchronously right
after hydration.
Pitfall
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:
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 10/12
28/2/24, 11:32 hydrateRoot – React
let i = 0;
setInterval(() => {
root.render(<App counter={i} />);
PREVIOUS
createRoot
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 11/12
28/2/24, 11:32 hydrateRoot – React
NEXT
Server APIs
©2024
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/client/hydrateRoot#hydrating-server-rendered-html 12/12