日本語

How React 19 Changed Hydration Error Messages

​​React​Playwright,Server-side rendering,TypeScript

This article was translated from the Japanese original with AI. 日本語の原文を読む

In React 19, when the server HTML and the browser render differ during hydration, the errors are combined into one error. That error includes the component path down to the mismatched element and the server-side and browser-side values as a diff. In React 18, a text or element-type mismatch printed four or five separate errors, and some of them did not include the values.

In a test environment, I rendered a page with a deliberate mismatch in React 18 and 19, opened it in Chromium, and recorded and compared the errors.

Note

The test environment was Ubuntu 24.04 and Node.js 22.22.0. I used react and react-dom 18.3.1 and 19.3.1, typescript 7.0.2, and esbuild 0.28.2. I type-checked the .tsx files with tsc and then transformed them with esbuild. I drove Chromium 141 with Playwright 1.56.1. Chromium ran in headless mode.

Point

In the React 19 development build, each mismatch produced one error. The end of the message has the component path from <App> to the mismatched element. A + line is the browser-side value and a - line is the server-side value. In React 18, values appeared in the message only for text and attribute mismatches. For an element-type mismatch, it only said that a <div> could not be found.

I made three kinds of mismatch and rendered them in React 18 and 19

Hydration is the process of attaching browser-side React to HTML that was created on the server (the official reference on hydrating server-rendered HTML). If the result the browser renders first differs from the server HTML, React reports an error.

For the test, I built a small component that imitates an order confirmation screen. The Status component checks typeof window to decide whether it is running on the server or in the browser, and deliberately renders something different in each. There are three kinds of mismatch: text, attribute (className), and element type.

tsx
const isServer = typeof window === 'undefined';

function Status({ scenario }: { scenario: Scenario }) {
  if (scenario === 'text') {
    // The text does not match
    return <p>描いた場所:{isServer ? 'サーバー' : 'ブラウザ'}</p>;
  }
  if (scenario === 'attr') {
    // Only the attribute (className) does not match
    return <p className={isServer ? 'status-server' : 'status-browser'}>発送の準備中です</p>;
  }
  // (omitted)
  if (scenario === 'element') {
    // The element type does not match
    return isServer ? <p>発送の準備中です</p> : <div>発送の準備中です</div>;
  }
  return <p>発送の準備中です</p>;
}

Outside Status there are two components, App and OrderSummary. App places a heading <h1> and OrderSummary inside a <main>. OrderSummary places a product name <p> and Status inside a <section>. On the server side, I created the HTML with renderToString. In the browser, I attached it with hydrateRoot as shown below. I did not pass any options, so the error output stays at the defaults.

tsx
hydrateRoot(document.getElementById('root')!, <App scenario={window.SCENARIO} />);

I used exactly the same files for React 18 and 19. I compiled the browser code in two ways: for development (NODE_ENV=development) and for production (NODE_ENV=production).

I recorded two kinds of output: what was printed with console.error, and the error events on window. I also recorded console.warn, but it had zero entries on every page. When React finds a mismatch during hydration and can keep going by rendering again in the browser, it reports that error (a recoverable error) with reportError by default. This happens when no onRecoverableError is passed to hydrateRoot. I confirmed this in the react-dom 18.3.1 and 19.3.1 code. According to the MDN description of reportError, reportError delivers the error to the console and the error event, the same way an uncaught exception would be delivered.

Number of errors in the development build

Each page had one mismatch. On pages without a mismatch, both versions recorded zero entries.

Mismatch React 18 React 19 DOM after hydration
Text 4 1 Replaced with the browser's text
Attribute 1 1 Kept the server's className
Element type 5 1 Replaced with the browser's <div>

The DOM after hydration was the same in 18 and 19. What differed was the number and content of the errors.

A text mismatch produces one error with a diff

In React 18, the following four entries were printed. Two console.error entries are followed by two error events. (I omitted some of the component path lines.)

text
[1] console.error:
Warning: Text content did not match. Server: "サーバー" Client: "ブラウザ"
    at p
    at Status (http://localhost:4173/client.js:24513:21)
    at section
    at OrderSummary (http://localhost:4173/client.js:24507:27)
(中略)
[2] console.error:
Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.
[3] window の error イベント:
Text content does not match server-rendered HTML.
[4] window の error イベント:
There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

The first entry has the values and the component path. The second says that the server HTML was replaced with the browser's render. To understand what mismatched and how React handled it, you need to read the first and second entries together. The third and fourth entries repeat almost the same content as the first and second, one sentence each, without the values.

In React 19, there was only one error event.

text
Hydration failed because the server rendered text didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:

- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

https://react.dev/link/hydration-mismatch

  <App scenario="text">
    <main>
      <h1>
      <OrderSummary scenario="text">
        <section>
          <p>
          <Status scenario="text">
            <p>
+             ブラウザ
-             サーバー

There are three improvements.

First, the first two sentences say what happened and what React did about it. They say that the server's text did not match the browser's, so this part will be rebuilt in the browser.

Second, the message includes a list of common causes. It lists branching on typeof window, Date.now() and Math.random(), date formatting that depends on the user's locale, invalid HTML tag nesting, and browser extensions. The mismatch in this test falls under the first item, branching on typeof window.

Third, the diff at the end. It shows the component path from <App> down to the mismatched <p>, including the props passed in (scenario="text"). The + line is the value rendered in the browser, and the - line is the value that was in the server HTML. In React 18, the first entry's component path contained only the parent chain. In React 19, the path also includes the surrounding elements, such as the <h1> and <p> before it, and the props passed to each component. This makes it easier to find where on the page the mismatch happened.

The React 19 release notes on the diff for hydration errors also say that what used to be several errors with no mismatch details is now one error with a diff.

One more note: the word "text" in the first sentence changes depending on the type of mismatch. According to the React changelog, starting in 19.2.0, a text mismatch says "text" instead of "HTML".

React 18 did not show values for element type mismatches

When a <p> came out as a <div>, React 18 produced five errors. (I omitted the component path lines.)

text
[1] console.error:
Warning: Expected server HTML to contain a matching <div> in <section>.
(中略)
[2] window の error イベント:
Hydration failed because the initial UI does not match what was rendered on the server.
[3] console.error:
Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.
[4] window の error イベント:
Hydration failed because the initial UI does not match what was rendered on the server.
[5] window の error イベント:
There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

The first entry says that a <div> was expected inside the <section> but was not found. It does not say what was actually in the server HTML (a <p>). The second and fourth entries are the same sentence. The third entry's <div> refers to the root <div id="root"> being hydrated, not to the mismatched <div>. The same third-entry sentence also appeared as the second entry in the text mismatch case.

In React 19, there was one error. The final diff was as follows. Apart from the first sentence, which now says "HTML" instead of "text", the explanation and the list of causes are the same as in the text mismatch case.

text
  <App scenario="element">
    <main>
      <h1>
      <OrderSummary scenario="element">
        <section>
          <p>
          <Status scenario="element">
+           <div>
-           <p>

Each line shows one side: <div> in the browser and <p> on the server.

Attribute mismatches are still not patched in 19

When only className differed, both versions printed one console.error entry. React 18 printed the following sentence, with the component path omitted.

text
Warning: Prop `className` did not match. Server: "status-server" Client: "status-browser"
(部品の並びは略)

React 19 printed an explanation in the same format as the other mismatches, followed by the list of causes and the diff. The first sentence is different. It says that some attributes did not match and that this won't be patched up.

text
A tree hydrated but some attributes of the server rendered HTML didn't match the client properties. This won't be patched up. This can happen if a SSR-ed Client Component used:
(原因の一覧とリンクは文字の食い違いのときと同じなので略)
  <App scenario="attr">
    <main>
      <h1>
      <OrderSummary scenario="attr">
        <section>
          <p>
          <Status scenario="attr">
            <p
+             className="status-browser"
-             className="status-server"
            >
+             発送の準備中です

The className line is the one that differs. The last line, + 発送の準備中です, is the content of the <p>. The same text was already in the server HTML.

As the message says, after hydration the DOM class stayed status-server. React 18 behaved the same way. Unlike text and element mismatches, attribute mismatches are not re-rendered in the browser. In this test, attribute mismatches were the only case where nothing appeared in the error events and the error was printed only to console.error.

Production builds show short errors with only a code number

In production builds, both versions printed only a numeric error code and no diff. (In 19, the code also carries the kind of mismatch.) For a text mismatch, React 18 printed the following two error events.

text
Minified React error #425; visit https://reactjs.org/docs/error-decoder.html?invariant=425 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
Minified React error #423; visit https://reactjs.org/docs/error-decoder.html?invariant=423 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

React 19 printed one.

text
Minified React error #418; visit https://react.dev/errors/418?args[]=text&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

#425 means "Text content does not match server-rendered HTML." #423 means that the whole root was re-rendered in the browser. For an element-type mismatch, React 18 printed two errors, #418 and #423. React 19 printed one error, #418 with args[]=HTML. The React 19 #418 has the same sentence as the long message in the development build, and the value of args[] fills in the "text" or "HTML" part of the first sentence. I confirmed this in React's error code list. The same code #418 has a different sentence in React 18.3.1's error code list. It is "Hydration failed because the initial UI does not match what was rendered on the server.", which is the same sentence that appeared as the second and fourth entries in the development build for element-type mismatches. In production builds, attribute mismatches recorded nothing in either version.

To see the diff, you need to run the development build.

How to fix it after seeing the diff

In this test, the mismatch came from splitting what is rendered based on typeof window. If you want to show something that differs only in the browser, follow the official reference on rendering different content on the client and server. Render the same output as the server on the first pass, then change state in an effect and render again.

The official reference also notes that this approach renders the component twice, which slows hydration, and that the switch can be noticeable on slow connections. For a single element whose text or attribute cannot match, such as a date, it also introduces suppressHydrationWarning, which suppresses the error for that element. I did not test suppressHydrationWarning in this article.

The component I used in the test, which re-renders from an effect, is shown below.

tsx
function WhereRendered() {
  const [isClient, setIsClient] = useState(false);
  useEffect(() => {
    setIsClient(true);
  }, []);
  return <p>描いた場所:{isClient ? 'ブラウザ' : 'サーバー'}</p>;
}

On the page that used this component, React 18 and 19 both recorded zero entries, in both the development and production builds. After hydration, the DOM showed the browser's value, "rendered in: browser".

​Posted by 86