Sub topic 8
Step 3: Find the minimal but complete state
This chapter assumes you can write function components, use JSX and props, and have built the static version of the product table from the previous step. Here you decide what data in the UI counts as state, and what does not.
Why minimal state matters
State is data that changes over time and that the UI must remember. Every extra piece of state is one more value you must keep in sync. If you store the same information twice, the two copies can drift apart and produce bugs.
The principle is DRY applied to state: keep the smallest set of state that fully describes the UI, and compute everything else from it. "Minimal" means no duplicates. "Complete" means the UI can be rebuilt from it with nothing missing.
List the data in the app
The searchable product table has these pieces of data:
- The original list of products.
- The search text the user typed.
- The value of the checkbox.
- The filtered list of products.
Which of these are state? Ask three questions about each one.
| Question | If yes |
|---|---|
| Does it stay unchanged over time? | It is not state. |
| Is it passed in from a parent through props? | It is not state. |
| Can you compute it from existing state or props? | It is not state. |
Only data that fails all three checks is state.
Apply the questions
The walkthrough below goes through each item.
- The original list of products. The component receives it as a prop. It is not state.
- The search text. It starts empty and changes as the user types. It is not passed in, and you cannot compute it from anything else. It is state.
- The checkbox value. It starts as
falseand changes when the user clicks. Nothing else determines it. It is state. - The filtered list. You can compute it from the products, the search text, and the checkbox value. It is not state.
The result is two pieces of state:
const [filterText, setFilterText] = useState('');
const [inStockOnly, setInStockOnly] = useState(false);Why the filtered list is not state
A common mistake is to store the filtered list in its own useState. It looks convenient, but now you have three values that must agree: the text, the checkbox, and the list. If you update the text and forget to update the list, the screen shows stale rows.
If you compute the list during render, it always matches the current text and checkbox. There is nothing to forget. The next chapter shows this computation in ProductTable.
Props are not state either
Props and state can look similar, because both are plain data that a component uses to render. They differ in who controls them. A parent controls props, and the child treats them as read-only. State belongs to the component that declares it, and that component can change it with a setter function.
In this app, products is a prop on every component that uses it. The two state values will be owned by a component higher in the tree, which the following chapters decide and build.
Check that the state is complete
Test the state against the mockup. Can you reproduce every visible variation from products, filterText, and inStockOnly?
- Empty search and unchecked box: all six products appear.
- Text
"pea": only Peas appear under Vegetables. - Checked box: Passionfruit and Pumpkin disappear.
- Both together: only rows that match both conditions remain.
Every screen follows from those three values, so the state is complete.
Common errors
- Copying a prop into state, such as
useState(props.products). The copy will not update if the parent passes a new list. - Storing the filtered list or a row count in state instead of computing them.
- Adding state for values that never change, such as column headings.
Exercise
The product table gains a footer showing "N products shown". Is N state? Explain using the three questions.
Show answer
No. N is not passed in as a prop and it changes over time, but it can be computed from the products, the search text, and the checkbox by counting the rows that pass the filter. Because it is computable from existing data, it should be calculated during render and not stored in state.