Sub topic 10
Pushing state back down when it no longer needs sharing
This chapter assumes you know useState, props, and callback props, and that you have already lifted state into a parent. Here you learn the reverse move: returning state to a child when the sharing requirement goes away.
Why state drifts upward
Lifting state is a response to a need. Two components must agree, so a parent owns the value. Requirements change, though. A feature is removed, a layout is redesigned, or a second consumer turns out to be unnecessary. The state stays in the parent anyway, and every render of that parent now carries data that only one child cares about.
Over-lifted state has real costs:
- The parent re-renders whenever the value changes, and so do all of its other children unless you memoize them.
- The parent's props and handlers grow, which makes it harder to read.
- A reader assumes shared state means shared logic, and goes looking for a second consumer that does not exist.
The rule of thumb
State should live in the lowest component that contains every component needing it. If only one component reads and writes a value, it belongs in that component. This is called colocation.
A worked example
Suppose a parent keeps both a search query and the open or closed state of a filter menu. Only FilterMenu uses isOpen.
import { useState } from 'react';
function SearchBox({ query, onQueryChange }) {
return (
<input value={query} onChange={e => onQueryChange(e.target.value)} />
);
}
function FilterMenu({ isOpen, onToggle }) {
return (
<div>
<button onClick={onToggle}>Filters</button>
{isOpen && <p>Filter options</p>}
</div>
);
}
export default function Toolbar() {
const [query, setQuery] = useState('');
const [isOpen, setIsOpen] = useState(false);
return (
<>
<SearchBox query={query} onQueryChange={setQuery} />
<FilterMenu isOpen={isOpen} onToggle={() => setIsOpen(!isOpen)} />
</>
);
}Every click on the Filters button re-renders Toolbar and SearchBox, even though the search box does not care. Push isOpen down:
import { useState } from 'react';
function SearchBox({ query, onQueryChange }) {
return (
<input value={query} onChange={e => onQueryChange(e.target.value)} />
);
}
function FilterMenu() {
const [isOpen, setIsOpen] = useState(false);
return (
<div>
<button onClick={() => setIsOpen(!isOpen)}>Filters</button>
{isOpen && <p>Filter options</p>}
</div>
);
}
export default function Toolbar() {
const [query, setQuery] = useState('');
return (
<>
<SearchBox query={query} onQueryChange={setQuery} />
<FilterMenu />
</>
);
}FilterMenu now takes no props. Toolbar shrinks to the one value that is shared. Toggling the menu re-renders only FilterMenu.
How to decide
Ask these questions about each piece of state in a parent:
- Which components read this value?
- Which components change it?
- If I removed the prop from every child but one, would anything else break?
If exactly one component answers all three, move the state there.
What changes when you move it
- Reset behavior. State in a child is lost when that child unmounts. If the menu was open, closed by hiding
FilterMenu, and reopened, it starts closed. State held in the parent survives. Decide whether you want that. - Outside access. The parent can no longer read or set the value. If it needs to, for example to close the menu when the user submits the search, the state must stay up.
- Siblings added later. If a new feature needs the value, lift it again. Moving state in either direction is a small edit.
Exercise
A parent holds const [hovered, setHovered] = useState(false) and passes hovered and setHovered to a single child, Card, which only uses them to change its own border style. What should you do, and what behavior changes if the parent hides and later re-shows Card?
Show answer
Move the hovered state into Card and remove both props. The parent no longer re-renders on hover. If the parent unmounts Card and mounts it again, hovered resets to false, because state in a child is lost when the child unmounts. The parent's old state would have persisted.