Sub topic 14
Lifting State Up to Share It
This chapter assumes you know function components, props, and the basics of useState, including updating state from event handlers. It covers what to do when two components need the same piece of state.
The problem: siblings that must agree
State belongs to the component that declares it. A sibling cannot read it, and a child cannot push data to a sibling. If two components must stay in sync, giving each its own useState produces two values that drift apart.
The fix is to lift the state up. Move it to the closest common parent, then pass it down as props. Pass a callback down too, so children can request changes.
Worked example: an accordion
In this accordion, only one panel is open at a time. Neither panel can know whether the other is open, so the parent owns a single activeIndex.
import { useState } from 'react';
function Panel({ title, isActive, onShow, children }) {
return (
<section>
<h3>{title}</h3>
{isActive ? <p>{children}</p> : <button onClick={onShow}>Show</button>}
</section>
);
}
function Accordion() {
const [activeIndex, setActiveIndex] = useState(0);
return (
<>
<Panel title="About" isActive={activeIndex === 0} onShow={() => setActiveIndex(0)}>
About text
</Panel>
<Panel title="Details" isActive={activeIndex === 1} onShow={() => setActiveIndex(1)}>
Details text
</Panel>
</>
);
}Panel now has no state of its own. It is a controlled component: the parent tells it what to show through isActive, and it reports clicks through onShow. Because only one number exists, two panels can never both be open.
The steps
- Find the components that need the same data.
- Pick their closest common parent.
- Declare the state there with
useState. - Pass the value down as a prop.
- Pass a callback that calls the setter, and have the child call it from an event handler.
Choosing where state lives
Put state in the lowest component that contains every component using it. Lifting higher than that re-renders more of the tree and forces extra props through components that do not care about the data.
| Situation | Where the state goes |
|---|---|
| Only one component reads and writes it | Keep it in that component |
| Two siblings share it | Their common parent |
| Distant components share it | Common ancestor, but consider whether passing props through many levels is becoming painful |
Common errors
- Duplicating state. If the parent holds
activeIndexandPanelcopiesisActiveinto its ownuseState, the copy will not update when the prop changes. Use the prop directly. - Calling the callback during render. Writing
onClick={onShow()}runs it immediately. Pass the function:onClick={onShow}. - Forgetting the callback. Passing only the value down leaves the child unable to change it.
- Lifting too far. Keep state close to where it is used until a second consumer appears.
Exercise
Two sibling components, SearchBox and ResultCount, each need the current search text. Where should the useState call go, and what does each sibling receive?
Show answer
Put it in their closest common parent. SearchBox receives the text as value and a callback such as onChange that calls the setter. ResultCount receives only the text as a prop. Neither sibling declares its own state for the text.