question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React 18: Updating state in an effect during mount causes hydration mismatch

See original GitHub issue

What version of React, ReactDOM/React Native, Redux, and React Redux are you using?

  • React: 18.0.0-alpha-88d121899-20210811
  • ReactDOM: 18.0.0-alpha-88d121899-20210811
  • Redux: 4.1.1
  • React Redux: 7.2.4

What is the current behavior?

When updating the store in an effect outside of a Suspense boundary, hydration mismatches are reported for components reading from the store inside Suspense boundaries.

Warning: Prop `className` did not match. Server: "language-js" Client: "language-ts"

Components reading the store outside of Suspense boundaries issue no hydration mismatches.

<Store>
  <Suspense fallback={null}>
    <Demo>inside suspense has hydration mismatch</Demo>
  </Suspense>
  <Demo>outside suspense has no hydration mismatch</Demo>
  <UpdatesStore />
</Store>

Example: https://codesandbox.io/s/react-18-updating-state-in-an-effect-during-mount-causes-hydration-mismatch-w7tjt?file=/src/App.js:2208-2372

The codesandbox includes an implementation using React Context as well where no problem is reported.

What is the expected behavior?

No hydration mismatches.

Which browser and OS are affected by this issue?

Chrome 92, Ubuntu 20.04

Did this work in previous versions of React Redux?

  • Yes

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Aug 12, 2021

I’d kinda like to see if this is reproducible with anything non-Redux-related.

Is this something I should do? Because I already tried with just React Context without success. At this point I’d have to dig into how react-redux works.

1reaction
eps1loncommented, Aug 12, 2021

I’m kind of confused by the “hydration” wording here. Is this SSR-related, or purely client-side?

SSR-related as shown in the codesandbox.

This seems like a React bug, not a React Redux bug.

Given that I see no hydration warnings using React Context, I don’t believe this is a React bug. But if you can reproduce the same issue with just React, we can file it against the React repository.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling the React server hydration mismatch error
The real problem is that our UI is “stale.” React doesn't update the UI to match the props that were rendered differently by...
Read more >
React v18.0 – React Blog
Batching is when React groups multiple state updates into a single re-render for better performance. Without automatic batching, we only batched ...
Read more >
How to solve react hydration error in Nextjs - Stack Overflow
This is quite a common issue in Next.js and a lot of times it's caused by incorrect JSX wrapping patterns. Check this question,...
Read more >
Avoiding hydration mismatch when using React hooks
One of the most difficult classes of bugs to track down and fix are those which appear seemingly at random. Bugs caused by...
Read more >
React 18: A Guide to Its New Features and Updates
React simulates mounting the component with the previous state. Layout effects are created. Effects setup code runs. Planning to Upgrade Your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found