Next.js Developer interview question · Most asked
What causes hydration mismatch errors and how do you fix them?
Summary
Hydration errors occur when the HTML rendered on the server differs from what React renders on the client's first pass, e.g. from using Date.now(), Math.random(), window/localStorage, locale-dependent formatting, or invalid HTML nesting.
Longer Answer
Fixes: render browser-only values in useEffect after mount, use dynamic(() => import(...), { ssr: false }) from a Client Component for browser-only components, pass deterministic data from the server, fix invalid nesting (e.g., <div> inside <p>), and use suppressHydrationWarning only for unavoidable cases like timestamps. Browser extensions modifying the DOM can also trigger them.
Key Points
- Server and client output must match
- Defer browser-only values to effects
dynamic(..., { ssr: false })- Fix invalid HTML nesting
More most-asked Next.js Developer questions
- How does data fetching work in the App Router?
- How did caching defaults change in Next.js 15?
- What happens, step by step, when a request reaches a Next.js App Router application?
- What is Next.js and what problems does it solve on top of React?
- What are the differences between the App Router and the Pages Router?
- What is the difference between Server Components and Client Components?
- Explain the App Router file conventions: page, layout, loading, error, not-found, template and route.
- Explain SSR, SSG, ISR and CSR in Next.js.