Sub topic 7
Fixing Mismatches with Client-Only Effects
This chapter assumes you know useState and useEffect, and that you have seen a hydration mismatch warning. Hydration is the step where React attaches to server-rendered HTML. If the first client render produces different output than the server HTML, React reports a mismatch.
Why effects help
An effect (useEffect) never runs on the server. During server rendering, React calls your component and produces HTML, but it skips effects. In the browser, effects run only after the first render has been committed, which is after hydration has compared the output to the server HTML.
This gives you a safe pattern. Render the same placeholder on the server and on the first client render. Then fill in the browser-only value inside an effect, which triggers a second render.
A clock that mismatches
The server and the browser compute different times, in different time zones and at different moments. The text in the HTML never matches the text the browser computes.
import { useEffect, useState } from 'react';
// Causes a mismatch: the server and the browser compute different text.
function BadClock() {
return <p>{new Date().toLocaleTimeString()}</p>;
}
// Fix: render the same placeholder on both, then fill in the time after hydration.
function Clock() {
const [now, setNow] = useState(null);
useEffect(() => {
setNow(new Date());
}, []);
return <p>{now ? now.toLocaleTimeString() : ''}</p>;
}In Clock, state starts as null. The server renders an empty <p>. The first client render also produces an empty <p>, so hydration matches. Then the effect runs, sets the time, and React re-renders with the real value.
What belongs in an effect
Use this approach for values that exist only in the browser or change between renders on different machines:
- The current time or date.
- Reads from
window,localStorage, ormatchMedia. - Viewport size and other layout measurements.
Do not read these during render. Code such as typeof window !== 'undefined' ? a : b also causes a mismatch, because the server takes one branch and the client takes the other.
Costs and limits
- Visible change. The user sees the placeholder first, then the real value. Reserve space for it, for example with a fixed-width element, to avoid layout shift.
- Extra render. Each effect-driven update costs one more render pass.
- Not for everything. If the value only needs a stable identifier, use
useId. If it comes from an external store such asnavigator.onLine, useuseSyncExternalStore. Both are covered in the next chapters.
Common errors
- Calling
setNowduring render instead of in the effect. This causes a render loop. - Rendering different placeholder markup on the server and client, for example
nullon one and<span></span>on the other. - Forgetting the empty dependency array, so the effect runs after every render.
Exercise
A component renders <p>{window.innerWidth}px</p>. It throws on the server and would mismatch in the browser. How do you rewrite it with an effect so that hydration matches?
Show answer
Store the width in state that starts as null, for example const [width, setWidth] = useState(null). In useEffect(() => { setWidth(window.innerWidth); }, []), set the real value. Render <p>{width === null ? '' : width + 'px'}</p>. The server and the first client render both output the empty placeholder, so hydration matches. The effect then updates the width after hydration.