Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Empty style={} object values cause hydration warnings in React 18.3.1 - Includes solution #30163

Open
nandastone opened this issue Jul 1, 2024 · 0 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@nandastone
Copy link

nandastone commented Jul 1, 2024

Issue

Hydrating a React 18 application with a style={} prop object with empty values results in a hydration warning:

function App() {
  const style = {
    color: "red",
    // Empty style value i.e. from conditional assignment.
    "--empty-custom-property": "",
  };

  return <p style={style}>Hello World</p>;
}

CleanShot 2024-07-01 at 21 07 54@2x

Repro: https://github.com/nandastone/react-18-19-ssr-empty-style

In the example above, the client generates a style object containing a property with an empty string value. This does not match the expected ReactDOM behavior which discards style object properties with empty values:

CleanShot 2024-07-01 at 21 01 47@2x

Example: https://codesandbox.io/p/sandbox/objective-tesla-nzsvwz?file=%2Fsrc%2FApp.js%3A8%2C1

Culprit

The issue lies in a line in the development-mode hydration check, called during diffHydratedProperties(): CSSPropertyOperations.js#L33

This check does not match the stricter ReactDOM implementation that correctly discards properties with empty values: dangerousStyleValue.js#L31

Solution

Update createDangerousStringForStyles() to match the ReactDOM implementation. This was addressed in React 19: CSSPropertyOperations.js#L34.

However, many applications (including mine) will be using React 18 for a while, so it would be good to fix this issue. Although the contributing guidelines suggest that all PRs should target main, is there a way to open a PR targeting the v18.3.1 tag?

React version: 18.3.1

Steps To Reproduce

  1. Render a component with a style prop where the object has a property with empty string value.
  2. Render the app using a React 18 server API e.g. renderToPipeableStream().
  3. Hydrate the app.

Link to code example:

https://github.com/nandastone/react-18-19-ssr-empty-style

  1. Clone.
  2. yarn
  3. yarn start
  4. http://localhost:9000

Unfortunately Codesandbox/Stackblitz were proving difficult to properly setup the SSR example.

The current behavior

Receive hydration warning.

The expected behavior

Should not receive hydration warning.

@nandastone nandastone added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jul 1, 2024
@nandastone nandastone changed the title Bug: Empty style={} object property values cause false hydration warnings in React 18.3.1 Jul 1, 2024
@nandastone nandastone changed the title Bug: Empty style={} prop values cause hydration warnings in React 18.3.1 Jul 1, 2024
@nandastone nandastone changed the title Bug: Empty style={} object values cause hydration warnings in React 18.3.1 Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
1 participant