Skip to content

Warning: Expected server HTML to contain a matching <div> in <div>. #17443

Answered by lfades
SergiiMost asked this question in Help
Discussion options

You must be logged in to vote

Code that is only supposed to run in the browser should be executed inside useEffect. That's required because the first render should match the initial render of the server. If you manipulate that result it creates a mismatch and React won't be able to hydrate the page successfully.

When you run browser only code (like trying to access window) inside useEffect, it will happen after hydration 👍

Replies: 14 comments 28 replies

Comment options

You must be logged in to vote
6 replies
@DavidVaness
Comment options

@aegiz
Comment options

@meotimdihia
Comment options

@tyrw
Comment options

@Pavan-Rajesh
Comment options

Answer selected by lfades
Comment options

You must be logged in to vote
16 replies
@Annukul
Comment options

@Leleking
Comment options

@Haliaa
Comment options

@Kogomre
Comment options

@Ys-OoO
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@alecrae
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@tuankietcoderr
Comment options

@meotimdihia
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@GodAlmighty990
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@omedinapr
Comment options

@lunarW1TCH
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet