Repository navigation
Why does my Next.js app show a hydration error when using dynamic data? #97437
Replies: 2 comments
|
The important distinction here is between a hydration mismatch and a state update that happens after hydration. With the simplified example, Initially, both the server and client render: <h1></h1>After hydration, This is a normal client-side update because it happens after hydration. A hydration error usually happens when the server's HTML differs from the client's initial render. Common causes are things like: Date.now()
Math.random()
new Date().toLocaleString()or browser-only APIs used during rendering: window.innerWidth
localStorage.getItem("theme")
document...Another common mistake is: const isBrowser = typeof window !== "undefined";
return isBrowser ? <ClientUI /> : <ServerUI />;This can make the server and client render different HTML. So the simple rule is:
Therefore, if the simplified |
|
fetch in |
Uh oh!
There was an error while loading. Please reload this page.
Summary
Why does my Next.js app show a hydration error when using dynamic data?
I'm building a Next.js application and I'm getting a hydration error when I fetch data from an API inside
useEffect.The simplified version looks like this:
All reactions