--- title: hydrateRoot --- `hydrateRoot` lets you display React components inside a browser DOM node whose HTML content was previously generated by [`react-dom/server`.](/reference/react-dom/server) ```js const root = hydrateRoot(domNode, reactNode, options?) ``` --- ## Reference {/*reference*/} ### `hydrateRoot(domNode, options?)` {/*hydrateroot*/} Call `hydrateRoot` to “attach” React to existing HTML that was already rendered by React in a server environment. ```js 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. [See more examples below.](#usage) #### Parameters {/*parameters*/} * `domNode`: A [DOM element](https://developer.mozilla.org/en-US/docs/Web/API/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 `` which was rendered with a `ReactDOM Server` method such as `renderToPipeableStream()`. * **optional** `options`: An object with options for this React root. * **optional** `onRecoverableError`: Callback called when React automatically recovers from errors. * **optional** `identifierPrefix`: A string prefix React uses for IDs generated by [`useId`.](/reference/react/useId) Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix as used on the server. * **optional** `nonce`: #### Returns {/*returns*/} `hydrateRoot` returns an object with two methods: [`render`](#root-render) and [`unmount`.](#root-unmount) #### Caveats {/*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. Use [`createRoot()`](/reference/react-dom/client/createRoot) instead. --- ### `root.render(reactNode)` {/*root-render*/} Call `root.render` to update a React component inside a hydrated React root for a browser DOM element. ```js root.render(); ``` React will update `` in the hydrated `root`. [See more examples below.](#usage) #### Parameters {/*root-render-parameters*/} * `reactNode`: A "React node" that you want to update. This will usually be a piece of JSX like ``, but you can also pass a React element constructed with [`createElement()`](/reference/react/createElement), a string, a number, `null`, or `undefined`. #### Returns {/*root-render-returns*/} `root.render` returns `undefined`. #### Caveats {/*root-render-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()` {/*root-unmount*/} Call `root.unmount` to destroy a rendered tree inside a React root. ```js 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-parameters*/} `root.unmount` does not accept any parameters. #### Returns {/*root-unmount-returns*/} `render` returns `null`. #### Caveats {/*root-unmount-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. --- ## Usage {/*usage*/} ### Hydrating server-rendered HTML {/*hydrating-server-rendered-html*/} If your app's HTML was generated by [`react-dom/server`](/reference/react-dom/client/createRoot), you need to *hydrate* it on the client. ```js [[1, 3, "document.getElementById('root')"], [2, 3, ""]] import { hydrateRoot } from 'react-dom/client'; hydrateRoot(document.getElementById('root'), ); ```` 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. ```html public/index.html

Hello, world!

``` ```js index.js active import './styles.css'; import { hydrateRoot } from 'react-dom/client'; import App from './App.js'; hydrateRoot( document.getElementById('root'), ); ``` ```js App.js import { useState } from 'react'; export default function App() { return ( <>

Hello, world!

); } function Counter() { const [count, setCount] = useState(0); return ( ); } ```
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.](/reference/react/useState) 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 during hydration. The most common causes leading to hydration errors include: * Extra whitespace (like newlines) around the React-generated HTML inside the root node. * Using checks like `typeof window !== 'undefined'` in your rendering logic. * Using browser-only APIs like [`window.matchMedia`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) in your rendering logic. * Rendering different data on the server and the client. React can recover from some hydration errors, but **you must fix them like other bugs.** In the best case, they'll lead to a slower app; in the worst case, event handlers would get attached to the wrong elements. --- ### Hydrating an entire document {/*hydrating-an-entire-document*/} Apps fully built with React can render the entire document from the root component, including the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html) tag: ```js {3,13} function App() { return ( My app ); } ``` To hydrate the entire document, pass the [`document`](https://developer.mozilla.org/en-US/docs/Web/API/Window/document) global as the first argument to `hydrateRoot`: ```js {4} import { hydrateRoot } from 'react-dom/client'; import App from './App.js'; hydrateRoot(document, ); ``` --- ### Suppressing unavoidable hydration mismatch errors {/*suppressing-unavoidable-hydration-mismatch-errors*/} If a single element’s attribute or text content is unavoidably different between the server and the client (for example, a timestamp), you may silence the hydration mismatch warning. To silence hydration warnings on an element, add `suppressHydrationWarning={true}`: ```html public/index.html

Current Date: 01/01/2020

``` ```js index.js import './styles.css'; import { hydrateRoot } from 'react-dom/client'; import App from './App.js'; hydrateRoot(document.getElementById('root'), ); ``` ```js App.js active export default function App() { return (

Current Date: {new Date().toLocaleDateString()}

); } ```
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. --- ### Handling different client and server content {/*handling-different-client-and-server-content*/} 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](/reference/react/useState) like `isClient`, which you can set to `true` in an [Effect](/reference/react/useEffect): ```html public/index.html

Is Server

``` ```js index.js import './styles.css'; import { hydrateRoot } from 'react-dom/client'; import App from './App.js'; hydrateRoot(document.getElementById('root'), ); ``` ```js App.js active import { useState, useEffect } from "react"; export default function App() { const [isClient, setIsClient] = useState(false); useEffect(() => { setIsClient(true); }, []); return (

{isClient ? 'Is Client' : 'Is Server'}

); } ```
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. This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections. The JavaScript code may load significantly later than the initial HTML render, so rendering a different UI immediately after hydration may also feel jarring to the user. --- ### Updating a hydrated root component {/*updating-a-hydrated-root-component*/} After the root has finished hydrating, you can call [`root.render`](#root-render) to update the root React component. **Unlike with [`createRoot`](/reference/react-dom/client/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.](/learn/preserving-and-resetting-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: ```html public/index.html

Hello, world! 0

``` ```js index.js active import { hydrateRoot } from 'react-dom/client'; import './styles.css'; import App from './App.js'; const root = hydrateRoot( document.getElementById('root'), ); let i = 0; setInterval(() => { root.render(); i++; }, 1000); ``` ```js App.js export default function App({counter}) { return ( <>

Hello, world! {counter}

); } ```
It is uncommon to call [`root.render`](#root-render) on a hydrated root. Usually, you'll [update state](/reference/react/useState) inside one of the components instead.