The Symptom: Stale Derived Values
What you'll learn: How storing derived state inside effects creates bugs where your UI displays outdated computed values between renders.
The Core Problem
When you calculate a value from props or state inside a useEffect and store it in a separate state variable, you create a synchronization gap. The derived value lives in state, but it's one render behind the source data it depends on. Between the time React renders your component and when the effect runs, your UI shows stale information.
Why This Happens
Remember: effects run after the browser paints. So the sequence becomes:
- Props/state change
- Component renders with new source data
- Browser paints the UI
- Effect runs and updates the derived state
- Component re-renders again to show the updated derived value
That gap between steps 3 and 5 is when users see outdated computed values.
A Concrete Example
Imagine you have a searchTerm prop and want to display searchTermLowercase. If you put the .toLowerCase() call inside an effect that sets state, your component renders twice: once with the old lowercase value, then again after the effect updates it. Users might see a flicker or incorrect filtering results during that first render.
This is exactly the problem we described earlier as "deriving state" — you're creating a copy of computed data instead of calculating it during render.
Key Takeaway: Derived values stored in effects lag behind their source data, causing the UI to display stale computed values until the effect runs and triggers a second render.