Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 132 ยท State and Events

Lifting State Up and Sharing State

Learn how to lift state up in React - controlled vs uncontrolled components, a single source of truth, accordion and tabs examples, prop drilling and when to stop.

State belongs to the component that calls useState. That is perfect until two components need to show or change the same piece of information. A temperature that appears in two input boxes, an accordion where opening one panel closes the others, a filter box that controls a list somewhere else: none of these can work if each component keeps its own private copy.

The standard answer in React is lifting state up: move the state to the closest common parent of the components that need it, and pass it down as props, together with functions to change it. The parent becomes the owner, the children become views. This lesson explains the idea with a mental picture, builds three examples, and then discusses the point where passing props through many layers stops being pleasant.

The problem: two copies drift apart

Here are two panels. Each has its own isActive state, so they know nothing about each other.

Two panels, two private states

You can open both panels at once, because each decides for itself. Now suppose the requirement is "only one panel open at a time". Panel A cannot close Panel B, because B's state lives inside B and nothing reaches it from outside. We need a place that knows about both.

The three steps of lifting

  1. Remove the state from the child components.
  2. Pass the data down from the common parent as props, along with the callbacks the children need to request changes.
  3. Add state to the common parent.

Picture the component tree. Before lifting, the information is hidden in the leaves. After lifting, it sits in the parent, and arrows of props point down while "please change this" callbacks travel back up through event handlers.

TEXT
App  <-- owns activeIndex
 |-- Panel (isActive = activeIndex === 0, onShow = () => setActiveIndex(0))
 |-- Panel (isActive = activeIndex === 1, onShow = () => setActiveIndex(1))

Data flows down as props; changes flow up as events. This one-way flow is why React apps are predictable: for any piece of shared data, there is exactly one place where it can change.

An accordion: state lifted into the parent

Panel no longer owns anything. It is told whether it is active and what to call when its button is pressed. Accordion holds a single number, activeIndex, so "two panels open at once" is not just prevented, it is unrepresentable: the state has room for only one answer. This ties back to choosing a state structure: one variable instead of three booleans.

Controlled and uncontrolled components

The Panel change has a name. A component with its own state is uncontrolled: its parent cannot influence it. A component that gets its important information from props is controlled: the parent drives it. You already know this from form inputs, where value and onChange hand control of a text box to React state.

Uncontrolled Controlled
Where the state lives Inside the component In the parent, passed as props
Easy to use Yes, no props needed Needs value + onChange style props
Parent can read or reset it No Yes
Parent can coordinate with siblings No Yes
Typical examples Simple widget with local toggle Form fields, tabs, accordions, sortable lists

Neither is wrong. Local state is simpler when nobody else cares. Lift only when a second component needs the information, or when the parent must be able to control or reset the child. A good rule of thumb: put state as low in the tree as possible, and as high as necessary. Later, the lesson on controlled vs uncontrolled components shows how to build components that support both modes.

Example: two inputs, one value

The classic demonstration is two views of one value. Here a temperature can be typed in Celsius or Fahrenheit, and the other box follows. Instead of storing both, we store one number and derive the other; each input is controlled by the parent.

Celsius and Fahrenheit sharing one source of truth

Type 212 into the Fahrenheit box and the Celsius box shows 100. Notice what is not here: no effect that copies one box into the other, and no second state variable to synchronise. The text exactly as typed is kept for the box the user is editing (so a half-typed 1. is not rewritten under their cursor) and the other box is calculated. Wherever you are tempted to "sync" two states with an effect, ask whether lifting one state and deriving the rest is simpler. It nearly always is, and You Might Not Need an Effect shows many more cases.

Example: tabs that other parts of the page can read

Lifting also lets a parent use the child's state. Here the page title and a status line both depend on which tab is selected, something a tab component with private state could never provide.

Tabs plus a header that reads the selected tab

The "Back to inbox" button sits outside TabBar and still changes the selected tab. That works because the owner is Mailbox. Also notice onSelect={setSelectedId}: a state setter is a function, so you can pass it directly as the callback when it needs no changes.

What a child may and may not do

Lifting creates a contract between parent and child. Keep to these habits:

  • Children never mutate props. They call the callback and let the parent produce the new state. Props are read-only, as described in props.
  • Name callbacks by intent. The prop is onSelect, onShow, onChange (what happened), while the parent's handler is handleSelect (what to do). Parents decide the consequence.
  • Pass the smallest thing needed. isActive and onShow is a better API than handing the whole activeIndex and setActiveIndex to every panel, because the panel stays reusable and cannot break the others.
  • Keep one owner. If two components both seem to "own" the same data, you have duplicated state. Pick one owner and derive the rest.

Prop drilling and when to stop

Lifting has a cost: the state may need to travel through components that do not care about it, only to reach one that does. Passing a prop through many layers is called prop drilling.

JSX
function App() {
  const [user, setUser] = useState(null);
  return <Layout user={user} />;
}
function Layout({ user }) {
  return <Sidebar user={user} />;      // does not use user
}
function Sidebar({ user }) {
  return <Avatar user={user} />;       // does not use user either
}
function Avatar({ user }) { /* finally uses it */ }

Two layers of pass-through are usually fine and are often a feature: you can see where data comes from. When it grows painful, you have options, in this order:

  1. Restructure with composition. Pass the finished element down instead of the data: <Layout avatar={<Avatar user={user} />} />. The intermediate components never see user. See children and composition; this removes a surprising amount of drilling.
  2. Move the state back down. If only a small subtree needs the value, lift it just far enough, no further.
  3. Use context for data many distant components need, such as the theme or the signed-in user: Context.
  4. Use an external store for large shared client state (external stores, state management libraries).

Do not jump to step 3 or 4 on day one. Context makes the flow of data less visible and can cause wide re-renders. Start with plain props and lift only as much as you must.

Where does state live? A quick checklist

  1. Identify every component that shows or changes the data.
  2. Find their closest common parent.
  3. The parent, or a component above it, owns the state.
  4. If no component fits, create a new one just to hold the state above them.
  5. Pass the data down; pass event callbacks (or a reducer's dispatch, see reducers) to the components that change it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Two sibling components need to read and change the same value. Where should the state live?

    Lifting state up means moving it to the nearest common parent, which passes the value and the change callbacks down as props.
  2. 2. How does a child component ask the parent to change the lifted state?

    Props are read-only. The parent passes a callback (often wrapping a state setter) and the child calls it from an event handler.
  3. 3. In the accordion, why store activeIndex in the parent instead of an isActive boolean in each panel?

    With one source of truth, 'only one panel is open' is guaranteed by the shape of the state, not by extra synchronisation code.
  4. 4. Which is usually the first thing to try when props are drilled through many layers?

    Composition avoids passing data through components that do not need it. Context and stores are for data many distant components use.
  5. 5. What does it mean that a component is controlled?

    A controlled component receives its value (and a way to request changes) from the parent instead of keeping private state.

Summary

  • When several components need the same changing data, lift the state to their closest common parent and pass it down as props.
  • Data flows down as props; changes flow up through callbacks called from event handlers.
  • A controlled component gets its value from the parent; an uncontrolled one keeps its own. Keep state low, lift only when needed.
  • Keep a single source of truth and derive everything else, instead of syncing copies with effects.
  • Prop drilling is acceptable at a couple of levels; try composition and moving state back down before context or a store.
  • Give children small, intention-revealing props such as isActive and onShow.

Next: useReducer and Reducers