Full answer
Every extra piece of state is something you must keep consistent. When one value can be calculated from another, store the source and calculate the rest.
Anti-pattern 1: mirroring a prop in state. const [name, setName] = useState(props.name) reads the prop only on the first render, so when the parent passes a new name the component still shows the old one. Fix options: use the prop directly (a controlled component), or if the component really needs an editable draft, name the prop initialName, and reset with key when the source changes (<Editor key={user.id} initialName={user.name} />).
Anti-pattern 2: storing computed data. Keeping filteredCourses in state and updating it in onChange or in an effect doubles the state, and any path that updates courses without updating filteredCourses leaves them inconsistent. Store the courses and the query, then compute const visible = courses.filter(...) in render. If the computation is slow, wrap it in useMemo after measuring, or let the React Compiler memoize it.
Anti-pattern 3: storing objects that duplicate others. const [selectedCourse, setSelectedCourse] = useState(course) copies the object, which goes stale if the list item is edited. Store the selectedId and derive the object with find.
Anti-pattern 4: syncing with effects. useEffect(() => setTotal(items.length * price), [items, price]) renders twice and briefly shows a wrong total. Compute it in render.
When copying is fine: you want a snapshot or a draft that intentionally diverges from the source (a form being edited), and you document and reset it.
A guideline: store the minimum state that cannot be computed, and avoid redundant, contradicting or duplicated state.
The example stores only the query and the selected id, and derives the rest.
import { useState } from 'react';
const COURSES = [
{ id: 1, title: 'Science basics' },
{ id: 2, title: 'Maths basics' },
{ id: 3, title: 'Science advanced' },
];
export default function CourseSearch() {
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState(null);
// Derived during render from the minimal state.
const visible = COURSES.filter((course) => course.title.toLowerCase().includes(query.toLowerCase()));
const selected = COURSES.find((course) => course.id === selectedId);
return (
<div>
<input aria-label="Search" value={query} onChange={(e) => setQuery(e.target.value)} />
<ul>
{visible.map((course) => (
<li key={course.id}>{course.title}</li>
))}
</ul>
<p>
{visible.length} of {COURSES.length} shown, selected: {selected ? selected.title : 'none'}
</p>
<button onClick={() => setSelectedId(3)}>Select course 3</button>
</div>
);
}The component stores only the query and the selected id. The check typed "sci", and the list and the count followed it with no effects and no duplicate state. Output of the run: start: Science basics, Maths basics, Science advanced | 3 of 3 shown, selected: none after typing sci: Science basics, Science advanced | 2 of 3 shown, selected: none
Key points
- Derived state is anything you can compute from existing state or props. Compute it during render.
- Copying a prop into useState ignores later prop changes. Use the prop, or name it initial and reset with a key.
- Store ids instead of copies of objects, and derive the object with find.
- Do not sync derived values with effects. It adds renders and flashes of wrong data.
- Copying is acceptable for drafts that intentionally diverge from the source.
Common mistakes
- Writing useState(props.value) and expecting it to follow prop changes.
- Storing filtered or sorted arrays in state.
- Storing a copy of a selected object instead of its id.
- Using useEffect to keep two pieces of state in sync.
- Wrapping every derived value in useMemo without measuring.
Follow-ups
- How would you build an editable form that starts from server data and resets on navigation?
- When is useMemo appropriate for derived data?
- How does the React Compiler affect this advice?
Tips
- Say "single source of truth" and "minimal state".
- Give the id instead of object example. It is easy to remember.
- Mention the key reset as the clean fix for drafts.