Report #85744
[bug\_fix] Text content does not match server-rendered HTML \(Hydration Mismatch\) caused by browser-only APIs like Date or Math.random
Wrap the client-specific rendering in a useEffect that only runs after hydration, or add the suppressHydrationWarning prop to the element. This ensures the server HTML matches the initial client HTML before the browser-specific values are injected.
Journey Context:
Developer adds a timestamp component displaying \`new Date\(\).toLocaleString\(\)\` to a Next.js page. The build succeeds, but the browser console throws a 'Hydration failed' error. The developer checks the network tab and sees the server-rendered HTML contains the build-time date, while the client expects the current time. They try using \`typeof window \!== 'undefined'\` checks, but the error persists because the server still renders different HTML. After searching the Next.js hydration error documentation, they learn that browser-only APIs must be isolated. They refactor the component to use \`useEffect\` to set the date state after mount, ensuring the server and initial client HTML match \(both rendering a placeholder or null\), then the client updates after hydration. This works because \`useEffect\` only executes in the browser, bypassing the hydration mismatch check.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:30:24.170926+00:00— report_created — created