Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Lifting state up

How to share state between sibling components in React by moving it to their closest common parent, passing values down as props and changes up as callbacks.

When two components need to stay in sync, each one keeping its own copy of the data is a recipe for bugs. The fix is to lift the state up: remove it from the children, put it in their closest common parent, pass the current value down as props and pass a function down that lets the children ask for a change. The parent then owns the single source of truth and the children become controlled by it.

You do this constantly without naming it: an accordion where opening one panel closes the others, a filter box that narrows a list shown elsewhere, two inputs that show the same quantity in different units.

JSX
function Parent() {
  const [value, setValue] = useState('');
  return (
    <>
      <Editor value={value} onChange={setValue} />
      <Preview value={value} />
    </>
  );
}

Try it

An accordion needs to know which panel is open, so the information cannot live inside any one panel. The parent stores the index of the open panel and each panel receives isOpen and an onShow callback.

An accordion with one open panel

The second example keeps two inputs in sync. Storing both a Celsius and a Fahrenheit value would let them disagree, so the parent stores one number and each input derives its text from it.

Two inputs, one source of truth

Syntax

There is no API, only a refactoring recipe.

Step What you do
1. Find the common parent The lowest component that renders every component needing the data.
2. Move the state there Call useState (or useReducer) in the parent and delete the children's copies.
3. Pass values down Give children the data as props. They become controlled components.
4. Pass changes up Give children callbacks such as onChange or onShow; the child calls them from its event handlers.

The data flows down and events flow up. This is called one-way data flow, and it makes it easy to find where a value comes from.

Usage notes

  • Lift only as far as necessary. State that only one component uses should stay there. The common parent is the right place, not the root of the app.
  • Avoid duplicated state. If a value can be computed from other state, compute it (see Derived state). Only one thing in the tree should be the source for each fact.
  • Prop drilling appears when state is lifted high and passes through many layers. Options: restructure with children, use context, or a small store library.
  • A callback prop is a request. The parent decides whether to accept it, so it can validate, reject or transform the change.
  • Lifting is reversible. If only one child ends up using the state, move it back down. Doing so also stops unrelated siblings from re-rendering when it changes.
  • Parents re-render their children. Lifted state means the parent renders on every change; keep expensive children in memo if that becomes a problem.
  • Names signal direction: onSomething for props that receive events, handleSomething for the functions that implement them.