Report #6484
[bug\_fix] Text content does not match server-rendered HTML \(Hydration mismatch caused by Date.now\(\) or Math.random\(\) in JSX\)
Move the dynamic value generation into a useEffect hook with state initialization to a static placeholder \(e.g., useState\(null\)\), and only render the actual value after mount. Alternatively, use suppressHydrationWarning=\{true\} on the element if the difference is intentional and harmless.
Journey Context:
Developer adds a timestamp like \{new Date\(\).toISOString\(\)\} or an ID from \{Math.random\(\)\} directly in the component return. Everything works in development \(which often uses client-side rendering with Strict Mode double-mounting masking the issue\), but the production build with SSR crashes with 'Text content does not match'. The developer checks the Network tab and sees the server HTML contains one timestamp, while the browser console shows a different one. They initially try to fix it with typeof window checks, but still get hydration warnings because the server rendered a different tree structure. They eventually realize the value must be generated only after hydration completes, moving the logic into useEffect with a state variable that defaults to a static string like '' or 'loading', eliminating the mismatch.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:13:22.100022+00:00— report_created — created