Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Derived state

Why copying props or other state into new state causes bugs in React, and how to compute derived values during render, store ids instead of copies and reset with keys.

Derived state is any value that can be calculated from props or other state: a filtered list, a total, a full name, a "selected item" that is really one entry of a list. The rule of thumb is simple: if you can compute it from what you already have, do not store it. Calculate it in the body of the component on every render.

Storing a derived value creates two copies of the same fact, and keeping them in step is your job. The usual symptoms are a UI that shows stale data after props change, effects whose only purpose is to copy one state into another, and an extra render for every copy. A computed value cannot get out of sync, because it is never stored.

JSX
// Not this: a second copy that must be kept in sync
const [fullName, setFullName] = useState(first + ' ' + last);

// This: always correct
const fullName = first + ' ' + last;

Try it

The left card below copies the name prop into state. The state is created from the prop only on the first render, so it ignores later prop changes. The right card reads the prop directly. Press the button that changes the parent's name and compare them.

Copying a prop into state

Another common copy is the selected object. If you store the object itself, editing the list leaves the selection pointing at an old version. Store the id and look the object up while rendering.

Store the id, derive the object

Toggle the item: the status line and the counter update together because both are computed from the same list.

Syntax

Ways to handle a value that depends on other values:

Case Approach
Cheap calculation (formatting, filter, sum) Compute it in the component body.
Expensive calculation Wrap it in useMemo with the inputs as dependencies.
Selected item from a list Keep the id in state, derive the object.
Local state that should restart when a prop identity changes Give the component a different key.
Local state that must adjust when a prop changes, without remounting Store the previous prop and update during render, or restructure so state lives in the parent.
Intentional "initial value" Name the prop initialName or defaultName so readers know later changes are ignored.
JSX
function Editor({ initialText }) {
  const [text, setText] = useState(initialText); // fine, and the name says so
}

Usage notes

  • An effect that calls setState with a value computed from props or state is almost always a mistake. It renders twice and flashes the old value. See You might not need an effect.
  • Copying props into state is fine when you want the first value only (a draft the user will edit). Say so in the prop name.
  • Do not sort or filter state in place. items.sort() mutates the state array; compute [...items].sort(...) or use toSorted while rendering.
  • Derived values are cheap more often than you think. Measure before reaching for useMemo. A loop over a few hundred items takes microseconds.
  • Keep state minimal. State should hold the smallest representation of the truth, such as a list plus a filter string, not the filtered list.
  • Normalize for lookups. If you search by id repeatedly in a large list, store a map from id to item, or build it with useMemo.
  • Objects as derived state: a derived object is a new reference on each render, which matters when it is a dependency of an effect or a prop of a memoized child.