React · Senior · Question 6 of 75
Which code in render typically causes a hydration error?
- ✓Correct answer:
new Date().toLocaleTimeString()or atypeof window !== 'undefined'branch - 2
useStatewith an initial value from props passed from the server - 3Calling
useMemowith an empty dependency array in a client component
Explanation
The server and the client must render the same HTML. Time, random numbers, checking window, localStorage and the browser locale produce different results. Show client-only values after mounting (an effect or useSyncExternalStore with getServerSnapshot). For unavoidable text differences (a timestamp) there's suppressHydrationWarning.