Sub topic 10
Step 4: Decide where state should live
This chapter assumes you can write function components and JSX, pass props, and have a static version of the product table. You also need to know which pieces of data are state. In this example they are filterText and inStockOnly. The product list and the filtered rows are not state.
Knowing what the state is does not tell you which component should hold it. This step answers that question.
Why ownership matters
React uses one-way data flow. Data moves down from parents to children through props. A child cannot read a sibling's state, and it cannot change its parent's data directly. So wherever you put a piece of state, only that component and its descendants can use it.
For each piece of state, ask three questions:
- Which components render something based on this state?
- Which is their closest common parent?
- Is the state needed anywhere above that parent?
The state belongs in the closest common parent of every component that uses it. If no suitable parent exists, create a new component just to hold the state and place it above them.
Apply it to the product table
The hierarchy in this example looks like this:
Work through the two state values:
filterTextis displayed in the search input, which is inSearchBar. It is also used to filter rows inProductTable.inStockOnlyis displayed in the checkbox, which is inSearchBar. It is also used to filter rows inProductTable.
Both values are used by SearchBar and ProductTable. Their closest common parent is FilterableProductTable, so both values live there.
| State | Used by | Owner |
|---|---|---|
| `filterText` | `SearchBar`, `ProductTable` | `FilterableProductTable` |
| `inStockOnly` | `SearchBar`, `ProductTable` | `FilterableProductTable` |
This pattern is often called lifting state up. You start with state in a child, notice a sibling needs it, and move it to the parent.
Declare the state in the parent
Add the state to FilterableProductTable with useState, and pass the values down as props:
import { useState } from 'react';
function FilterableProductTable({ products }) {
const [filterText, setFilterText] = useState('');
const [inStockOnly, setInStockOnly] = useState(false);
return (
<div>
<SearchBar
filterText={filterText}
inStockOnly={inStockOnly}
/>
<ProductTable
products={products}
filterText={filterText}
inStockOnly={inStockOnly}
/>
</div>
);
}The initial values match the starting UI: an empty search and an unchecked box. The setter functions, setFilterText and setInStockOnly, are not passed down yet. The next step adds them as callback props.
If you run this now, the table and the search bar both receive the current values. Typing does nothing, because nothing can update the state. The parent owns the state, but the children cannot ask it to change.
Common mistakes
- Putting state too low. If
SearchBarkeptfilterTextitself,ProductTablecould never see it. You would have to copy it upward, which leads to two versions of the same value that can disagree. - Putting state too high. Keeping it in a component above
FilterableProductTableworks, but it forces more components to re-render and exposes the state to code that does not need it. - Duplicating state. Do not give
ProductTableits own copy offilterTextinitialized from the prop. The copy will not update when the prop changes. Keep one owner and pass the value down.
Check your understanding
Exercise
Suppose you add a ResultCount component that shows how many products match the filter. It is rendered next to ProductTable inside FilterableProductTable. Does filterText need to move? Explain.
Show answer
No. filterText already lives in FilterableProductTable, which is the parent of SearchBar, ProductTable, and ResultCount. It can pass filterText and inStockOnly to ResultCount as props. State only needs to move up when a component that needs it is not a descendant of the owner.
Exercise
A teammate stores filterText in SearchBar with useState, and also passes the typed value up to ProductTable by copying it into a second useState there. What is wrong with this design?
Show answer
The same data is stored in two places, so the two copies can fall out of sync. The state should live once, in the closest common parent FilterableProductTable, and be passed down as props to both children.