Sub topic 10
Pushing State Back Down When It No Longer Needs Sharing
Lifting state up fixes components that must agree. The opposite mistake is also common. Over time, state moves up the tree and stays there after the reason for lifting has gone. This chapter covers how to notice that, how to move state back down, and what you gain and risk by doing so.
This chapter assumes you can write function components with useState. It also assumes you know that props flow downward, that callbacks let a child request a change, and that you have already lifted state to a common parent at least once.
Why state ends up too high
State rises for good reasons. Two siblings need the same value, so it moves to their parent. Later the requirements change:
- One of the two consumers is deleted.
- A feature that shared the value is removed or redesigned.
- A value was lifted "just in case" and nothing else ever read it.
- A developer put all state in the top component to avoid thinking about ownership.
The code still works, which is why this goes unnoticed. The cost shows up as noise and wasted work. Every extra piece of state in a parent has three effects:
- Wider re-renders. When a component's state changes, React re-renders that component and, by default, all of its children. Children that never use the value still re-render.
- Longer prop lists. A value that only one child uses must still be passed down along with its setter, so the parent's JSX gets cluttered.
- Harder reasoning. A reader of the parent has to ask what every state variable is for. State that belongs to one widget is mixed in with state that is truly shared.
Nothing here is a bug. It is a design smell, and it is cheap to fix.
Spotting state that can move down
Ask two questions about each state variable in a component:
- Who reads it? List every component that uses the value, directly or after passing it through props.
- Who writes it? List every component that can call the setter, or a callback that calls it.
If the readers and writers all sit inside one child's subtree, the state does not need to live in the parent. The owner should be the closest component that contains all of them, and that may be the child itself.
| Situation | Where the state should live |
|---|---|
| Two siblings read or write the value | Their closest common parent |
| Only one child, and its descendants, use it | That child |
| The parent only needs the final result, not each change | The child holds a draft, and the parent receives the result through a callback |
| Many distant components need it | Consider context or another mechanism (a later chapter) |
A worked example: a search box in a dashboard
Here the parent holds a search query. Only SearchBox uses it, yet App owns it.
import { useState } from 'react';
function SearchBox({ query, onQueryChange }) {
return (
<label>
Search:{' '}
<input value={query} onChange={e => onQueryChange(e.target.value)} />
</label>
);
}
function Report() {
console.log('Report rendered');
return <p>Quarterly report (this component ignores the query).</p>;
}
export default function App() {
const [query, setQuery] = useState('');
return (
<>
<SearchBox query={query} onQueryChange={setQuery} />
<Report />
</>
);
}Report never reads query, but it logs "Report rendered" on every keystroke. The reason is that App re-renders when query changes, and Report is one of its children. In a real app, Report might be a large chart or table.
The fix is to move query into SearchBox. The component then owns its state and has no props to pass:
import { useState } from 'react';
function SearchBox() {
const [query, setQuery] = useState('');
return (
<label>
Search:{' '}
<input value={query} onChange={e => setQuery(e.target.value)} />
</label>
);
}
function Report() {
console.log('Report rendered');
return <p>Quarterly report (this component ignores the query).</p>;
}
export default function App() {
return (
<>
<SearchBox />
<Report />
</>
);
}Typing now re-renders only SearchBox. App has no state, so it does not re-render, and Report stays untouched. The parent lost two props and one useState call, and the code became easier to read.
The mechanism matters here. Pushing state down narrows the subtree that re-renders. It does not make any single render faster. You get the benefit only when the components left out of the update are expensive or numerous.
When the parent needs only the result
Some state looks shared but is not. A comment form is a typical case. The parent needs the list of submitted comments. It does not need every character typed into the draft.
import { useState } from 'react';
function CommentForm({ onSubmit }) {
const [draft, setDraft] = useState('');
function handleSubmit(e) {
e.preventDefault();
const text = draft.trim();
if (!text) return;
onSubmit(text);
setDraft('');
}
return (
<form onSubmit={handleSubmit}>
<input value={draft} onChange={e => setDraft(e.target.value)} />
<button type="submit">Post</button>
</form>
);
}
export default function Thread() {
const [comments, setComments] = useState([]);
return (
<>
<CommentForm onSubmit={text => setComments([...comments, text])} />
<ul>
{comments.map((c, i) => (
<li key={i}>{c}</li>
))}
</ul>
</>
);
}The state is split by who needs it:
commentsis read by the list and written on submit, soThreadowns it.draftis read and written only inside the form, so the form owns it.
If draft lived in Thread, the whole list would re-render on every keystroke. With a long list that is a measurable cost. The callback still works as before: the child hands up a finished value and the parent decides what to do with it.
Keeping a draft local has a limit. If another component must preview the draft live, as in a "preview your comment" pane beside the form, then the draft is shared again and belongs in the common parent. Decide by who needs the value, not by habit.
Edge cases and trade-offs
Local state is lost when the component unmounts
State lives as long as its component stays in the tree at the same position. When the component is removed, its state is discarded. Consider a tabbed view where the active tab renders its own panel:
import { useState } from 'react';
function NotesPanel() {
const [notes, setNotes] = useState('');
return <textarea value={notes} onChange={e => setNotes(e.target.value)} />;
}
export default function Tabs() {
const [tab, setTab] = useState('notes');
return (
<>
<button onClick={() => setTab('notes')}>Notes</button>
<button onClick={() => setTab('help')}>Help</button>
{tab === 'notes' ? <NotesPanel /> : <p>Help text.</p>}
</>
);
}Switch to Help and back, and the notes are gone, because NotesPanel unmounted and mounted again with fresh state. If users expect the text to survive, notes has to live above the conditional, in Tabs or higher. This is a legitimate reason to keep state high. Pushing down is correct only when losing the state on unmount is acceptable.
Resetting local state on purpose
The same behavior can be useful. When you want a component to start over, change its key:
<CommentForm key={threadId} onSubmit={handleSubmit} />When threadId changes, React treats this as a different component. It unmounts the old form and mounts a new one with an empty draft. This replaces effects that try to reset state by hand, and it only works when the state is local to the component being re-keyed.
Moving state down can break a hidden reader
Before you move state, search for every use of the variable. If a distant component reads it through a long chain of props, moving it down breaks that chain. Your editor's "find references" feature is useful here. After the move, the compiler or linter should report any unresolved identifier. In plain JavaScript you may only find the problem at runtime, so test the screens that used the old prop.
Do not push down below the actual consumers
Moving state too far is also an error. If you put query inside a deeply nested SearchIcon that merely displays it, a second reader elsewhere has no access to it. The target is the closest component that contains every reader and writer, no higher and no lower.
The decision flow
A step-by-step procedure
- Pick one
useStatecall in a parent component. - List every place that reads the variable or calls its setter.
- If all of them are in one child's subtree, move the
useStatecall into that child. - Remove the matching props from the child's signature and from the parent's JSX.
- If the parent still needs a result, add a single callback prop that reports it, as in
onSubmitabove. - Run the screen and confirm that the behavior is unchanged, including what happens when the component is hidden and shown again.
Summary of the rule
State should live in the closest component that contains everything that reads or writes it. Lifting moves it up when a new reader appears. Pushing down moves it back when readers disappear. Revisit ownership whenever a feature is added or removed, because the right owner changes with the code around it.
Exercise
A Page component holds const [open, setOpen] = useState(false) and passes open and setOpen only to its child Menu. Page has two other children, Header and Footer, that do not use open. What should you change, and what visible effect does it have on Header and Footer when the menu toggles? Name one situation in which you should not make the change.
Show answer
Move the useState call into Menu and delete the open and setOpen props from Page's JSX and from Menu's signature. After the move, toggling the menu re-renders only Menu, so Header and Footer no longer re-render when it toggles, because Page no longer has changing state. Do not make the change if Menu can be unmounted, for example by conditional rendering, and the open or closed state must be remembered when it returns, or if another component such as Header will need to read open. In those cases the state must stay in Page or in another component that stays mounted.
Exercise
In the Thread example, you want a "preview" pane beside the form that shows the draft text as the user types. Where should draft live now, and what props change?
Show answer
The draft is now read by two siblings, CommentForm and the new preview pane, so it must be lifted to their closest common parent, Thread. Thread holds const [draft, setDraft] = useState(''). It passes draft and setDraft (or an onDraftChange callback) to CommentForm, and passes draft to the preview pane. CommentForm becomes a controlled component and no longer calls useState for the draft.