Sub topic 7
State versus props: what changes over time
This chapter assumes you can write a function component, read JSX, and pass props to a child. It builds on the static product table from the previous step and sets up the next one, where you decide what the table's state actually is.
Two kinds of data
React components work with two kinds of data. Both are plain JavaScript values, and both affect what a component renders. They differ in who owns them and whether they can change.
- Props are values a parent passes to a child. The child reads them and never modifies them.
- State is a value a component remembers between renders. The component owns it and changes it by calling a setter function.
In the static version, every component received everything through props. ProductRow got a product, and ProductTable got a products array. Nothing changed after the first render, so no state was needed.
Props: a snapshot from above
Props work like function arguments. When the parent renders, it hands each child a set of values, and the child draws its output from them.
function ProductRow({ product }) {
// Reading is fine. Writing is not.
// product.price = '$0'; // Never do this.
return (
<tr>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
);
}If a child needs different values, the parent must render again and pass new props. The child cannot reach up and change them.
State: memory that triggers a render
State is for data that changes in response to the user. When you call a setter such as setFilterText, React stores the new value and renders the component again. The new render then sees the new value.
import { useState } from 'react';
function FilterBox() {
const [filterText, setFilterText] = useState('');
return (
<input
value={filterText}
onChange={(e) => setFilterText(e.target.value)}
/>
);
}A plain variable would not work here. Changing a local variable does not tell React to render again, and the variable resets on every render anyway.
Props and state together
The same value can be state in one component and a prop in another. The component that calls useState owns the value. Any component that receives it from above sees it as a prop.
In the finished product table, filterText is state inside FilterableProductTable. Inside SearchBar and ProductTable, it is a prop. Those two components cannot tell where the value came from, and they do not need to.
Telling them apart
Props and state answer different questions. The table below summarizes the differences.
| Question | Props | State |
|---|---|---|
| Who sets it? | The parent | The component itself |
| Can the component change it? | No | Yes, through the setter |
| Does it persist between renders? | Only if the parent keeps passing it | Yes |
| Typical use | Configuration and data from above | Data that changes with user input |
What changes over time
The key question is whether a value changes while the app runs. Apply it to the product table.
- The list of products does not change as the user types. It arrives from outside, so it stays a prop.
- The text in the search box changes with every keystroke. It must be remembered, so it is state.
- The checkbox value changes when clicked. It is also state.
Not every changing value should become state, though. A value that you can compute from other values is not state. The next chapter covers that test in detail.
Exercise
A Counter component shows a number and a button. Clicking the button adds one. A parent renders <Counter label="Clicks" />. Which of label and the number is state, and which is a prop?
Show answer
label is a prop because the parent supplies it and the Counter never changes it. The number is state because it changes over time when the user clicks, and the Counter must remember it between renders.
Exercise
A developer writes let filterText = '' inside FilterBox and updates it in onChange. The input never updates on screen. Why, and what is the fix?
Show answer
Changing a local variable does not make React render again, and the variable is reset to '' on every render. The fix is const [filterText, setFilterText] = useState('') and calling setFilterText in onChange.