Sharing state between components

Intermediate

Introduction

State belongs to one component, but interfaces often need two components to agree: only one accordion panel should be open, a total should change when any item changes, two fields should show the same distance in different units. When components need to share state, you lift it up: remove it from the children and keep it in their closest common parent, which passes it back down.

How it works

Suppose each Panel has its own isOpen state. Each can open independently, but nothing stops two from being open at once, because neither knows about the other. To make them coordinate:

  1. Remove the state from the children.
  2. Add it to the closest common parent: const [openIndex, setOpenIndex] = useState(0).
  3. Pass each child what it needs as props: isOpen={openIndex === 0}.
  4. Pass a callback prop so the child can ask for a change: onShow={() => setOpenIndex(0)}.

The children are now controlled components: they show what the parent says and report events, and the parent decides what changes. This is the same pattern as a controlled input, which shows the value it is given and reports onChange.

The goal is a single source of truth: each piece of data is stored in exactly one component, and everything else gets it through props or computes it. Two copies of the same data in two components will eventually disagree. When a value can be computed from others, such as miles from kilometres, store one and compute the other during render.

Where should state live? Find every component that reads it or changes it, find their closest common parent, and put it there (or higher, if a parent further up also needs it). State that only one component uses can stay in that component. Lifting state too high has a cost: every change renders more of the tree, and props have to be passed through components that do not use them, which a later lesson solves with context.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. Panel has no state: it shows its text when isOpen is true, and a Show button otherwise.
  2. App owns openIndex, the one place that says which panel is open.
  3. Each panel gets isOpen computed from openIndex, and an onShow callback.
  4. Clicking Show in the second panel sets openIndex to 1, so the first closes and the second opens in the same render.

Expected result

The preview shows the About panel open with its text and the Courses panel with a Show button. Clicking Show opens Courses and closes About.

In the real world

Tabs, accordions, step-by-step wizards, filter panels next to result lists, and shopping carts next to product pages all share state through a common parent. Deciding where state lives is the core skill of React design; the official guidance is to keep state as close as possible to where it is used, and lift it only as far as necessary.

Common mistakes

  • Copying props into state in the child, which then stops following the parent.
  • Keeping the same data in two components, which fall out of step.
  • Lifting state far higher than needed, so large parts of the app render on every change.
  • Passing the setter itself to children when a specific callback, such as onShow, would be clearer.

Best practices

  • Put state in the closest common parent of the components that use it.
  • Name callback props after what happened: onShow, onChange, onAdd.
  • Store the minimum and compute the rest during render.

Summary

To share state, lift it to the closest common parent, pass the value down as props and pass callback props so children can request changes. The children become controlled components, the parent is the single source of truth, and values that can be computed are computed rather than stored twice.

Quick check

1. Two sibling components need the same state. Where should it live?
2. How does a child ask its parent to change shared state?
3. What does single source of truth mean?
4. A km field and a miles field must agree. What should be state?

Key takeaways

  • When two components need the same state, move it to their closest common parent.
  • Pass the value down as a prop and a function to change it as a callback prop.
  • Store each piece of data in one place and derive the rest.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.