Derived state stored in useState
Quick summary
You stored a value in useState that is computed from props — it becomes stale when props change and causes synchronization bugs.
Why this happens
You copy a prop into useState and update it with useEffect, creating a redundant state that can go stale
A value that should be computed on each render is stored in state and only updated via an effect
You use state to cache a computation that could be derived directly from existing props or state
Minimal example
function FilteredList({ items, filter }) {
const [filtered, setFiltered] = useState([]);
useEffect(() => {
setFiltered(items.filter(i => i.type === filter));
}, [items, filter]); // derived state in useState
return filtered.map(i => <div key={i.id}>{i.name}</div>);
}function FilteredList({ items, filter }) {
const filtered = useMemo(
() => items.filter(i => i.type === filter),
[items, filter]
); // derived value, not state
return filtered.map(i => <div key={i.id}>{i.name}</div>);
}Using useMemo instead of useState + useEffect computes the derived value directly from props, eliminating the synchronization bug.
How to diagnose
Check if the state value is always equal to a function of props or other state — if so, it is derived state
Look for useEffect that syncs state to props — this pattern usually indicates derived state that should be computed instead
Verify if the state ever diverges from the computed value, which would indicate a synchronization bug
How to fix
Compute the value directly during render: const filtered = items.filter(...) instead of storing in state
Use useMemo to memoize expensive derived computations without storing them in state
If you need both the derived value and user overrides, store only the override in state and fall back to the derived value
How to prevent
Never store derived state in useState — compute it during render or memoize with useMemo
Avoid the anti-pattern of copying props to state and syncing with useEffect
Related Resources
Related Lessons
- State Patterns
Learn about state management.