Sub topic 6
Lifting State to the Closest Common Parent
This chapter assumes you know useState, function components, props, and onChange handlers. You will move state out of two sibling components and into their shared parent, so both inputs always show the same value.
The problem: two copies of the truth
Here each Field owns its own text state. Typing in one input never changes the other.
import { useState } from 'react';
function Field({ label }) {
const [text, setText] = useState('');
return (
<label>
{label}: <input value={text} onChange={e => setText(e.target.value)} />
</label>
);
}
export default function App() {
return (
<>
<Field label="First" />
<Field label="Second" />
</>
);
}React keeps one state slot per component instance. Data flows only downward through props, so a sibling cannot read another sibling's state. Two copies of the value will drift apart.
One owner per piece of state
A value that several components need should have a single owner. Everyone else receives it as a prop. That owner is the single source of truth.
The steps to lift state
- Remove the state from the children.
- Find the closest common parent of the components that need the value.
- Declare the state in that parent.
- Pass the value down as a prop.
- Pass a callback down as a prop so children can request changes.
The lifted version
import { useState } from 'react';
function Field({ label, value, onChange }) {
return (
<label>
{label}: <input value={value} onChange={e => onChange(e.target.value)} />
</label>
);
}
export default function App() {
const [text, setText] = useState('');
return (
<>
<Field label="First" value={text} onChange={setText} />
<Field label="Second" value={text} onChange={setText} />
</>
);
}Field no longer calls useState. It receives value and onChange and simply reports what the user typed. When setText runs, App re-renders and passes the new text to both fields.
Controlled inputs
An input whose value comes from props is a controlled component. React state, not the DOM, decides what the input shows. The parent owns the value, and the input is only a view of it plus a way to request changes.
A controlled Field is also less flexible in one way. It cannot hold text that the parent does not know about.
Which component should own the state?
Pick the closest common parent of every component that reads or writes the value. Going higher than that makes more components re-render and forces you to pass props through more layers. Going lower leaves some component without access.
Accordion panels follow the same rule. A rule such as "only one panel open at a time" cannot live in a single panel, so the parent stores one index.
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>
);
}
export default function Accordion() {
const [activeIndex, setActiveIndex] = useState(0);
return (
<>
<Panel
title="About"
isActive={activeIndex === 0}
onShow={() => setActiveIndex(0)}
>
Text about the project.
</Panel>
<Panel
title="Details"
isActive={activeIndex === 1}
onShow={() => setActiveIndex(1)}
>
More details here.
</Panel>
</>
);
}Each Panel is controlled by isActive. Opening one panel changes activeIndex, so the other panel closes without any extra code.
Common errors
- Keeping a copy in the child. Writing
useState(props.value)in a child copies the value once. Later changes from the parent are ignored. - Passing the call instead of the function. Writing
onShow={setActiveIndex(0)}runs the setter during render. WriteonShow={() => setActiveIndex(0)}. - Forgetting the callback. A
valuewithoutonChangegives a read-only input, and React warns about it.
Exercise
In the lifted App example, what would happen if Field kept const [text, setText] = useState(value) and showed text in the input instead of the value prop?
Show answer
Each Field would copy the initial value once and then edit its own copy. Typing in one field would no longer update the other, because the value prop is ignored after the first render. The two copies of the truth come back, so the Field should read value directly.