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

React 18 Bug: Hydration mismatch if empty string is rendered #22784

Closed
eps1lon opened this issue Nov 17, 2021 · 2 comments
Closed

React 18 Bug: Hydration mismatch if empty string is rendered #22784

eps1lon opened this issue Nov 17, 2021 · 2 comments
Labels
React 18 Bug reports, questions, and general feedback about React 18 Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug

Comments

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 17, 2021

React version: 18.0.0-beta-4ff5f5719-20211115

Steps To Reproduce

  1. Render empty string ReactDOMServer.renderToString("")

Link to code example: https://codesandbox.io/s/react-18-emptry-string-hydration-mismatch-xz4w9
Original issue: f9d729e (#1541)

Did some spelunking in the codebase and it seems to me that the reconciler is looking for a hydrateable instance (

if (current === null) {
tryToClaimNextHydratableInstance(workInProgress);
}
). But since an empty string won't appear as a text node (if we just set innerHTML = string), the reconciler thinks there's a mismatch. In legacy roots we didn't throw but warn unless we had an empty string:
if (text === '') {
// We expect to insert empty text nodes since they're not represented in
// the HTML.
// TODO: Remove this special case if we can just avoid inserting empty
// text nodes.
return;
}

The current behavior

Console error is logged with "An error occurred during hydration. The server HTML was replaced with client content"

The expected behavior

No hydration mismatch just like in React 17: https://codesandbox.io/s/react-17-emptry-string-no-hydration-mismatch-forked-5tgmw

@eps1lon eps1lon added Type: Bug Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug React 18 Bug reports, questions, and general feedback about React 18 labels Nov 17, 2021
@salazarm
Copy link
Contributor

Thanks for the report -- looking into possible solutions here

@eps1lon
Copy link
Collaborator Author

eps1lon commented Nov 23, 2021

Closed by #22807

@eps1lon eps1lon closed this as completed Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React 18 Bug reports, questions, and general feedback about React 18 Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug
Projects
None yet
Development

No branches or pull requests

2 participants