Sub topic 8
Props versus state: who owns the data
This chapter assumes you can write function components, pass props, and have used useState at least once. It covers how to decide whether a piece of data should be a prop or state.
Two kinds of data
Props and state both feed a component's output, but they differ in who controls them.
| Row | Props | State |
|---|---|---|
| Comes from | The parent component | The component itself |
| Can the component change it? | No, read-only | Yes, through the setter function |
| Changing it | The parent renders with new values | The setter schedules a re-render |
| Lifetime | Set on each render by the parent | Kept by React between renders |
A value that is state in one component becomes a prop when that component passes it down. The data is the same. What differs is which component owns it.
Ownership in practice
The component that calls useState owns the data. Children receive it as props and can only ask for changes through callback props.
import { useState } from 'react';
function Total({ count }) {
return <p>Total: {count}</p>;
}
function AddButton({ amount, onAdd }) {
return <button onClick={() => onAdd(amount)}>+{amount}</button>;
}
export default function Counter() {
const [count, setCount] = useState(0);
function handleAdd(amount) {
setCount((current) => current + amount);
}
return (
<div>
<Total count={count} />
<AddButton amount={1} onAdd={handleAdd} />
<AddButton amount={5} onAdd={handleAdd} />
</div>
);
}Counter owns count. Total and both buttons never change it directly. Because Total and the buttons need the same value, the state sits in their closest common parent. This is called lifting state up.
Deciding where data lives
Ask these questions in order:
- Does the value change over time? If not, it is a constant or a prop, not state.
- Can it be calculated from props or other state? If so, calculate it during rendering and do not store it.
- Which components need it? Put the state in their closest common parent.
For example, store items in state and compute items.length during rendering. A separate itemCount state would need to be kept in sync by hand, and eventually it would drift.
Common error: copying props into state
A frequent mistake is initializing state from a prop and expecting it to follow the prop.
import { useState } from 'react';
function Price({ amount }) {
// Wrong: amount is read once, on the first render only.
const [price] = useState(amount);
return <p>${price}</p>;
}
function PriceFixed({ amount }) {
// Right: read the prop directly.
return <p>${amount}</p>;
}When the parent passes a new amount, Price keeps showing the old value. React ignores the argument to useState after the first render. Copy a prop into state only when you want an initial value that the component then edits on its own. In that case, name the prop initialAmount so the intent is clear.
Controlled and uncontrolled behavior
A component such as a text input can work either way. If you pass value and onChange, the parent owns the data and the input is controlled. If you pass nothing and let the input track its own text, the input owns the data and is uncontrolled. Controlled components are easier to validate and reset. Uncontrolled ones need less code.
Exercise
A SearchPage component renders a SearchBox and a ResultsList. Both need the current query text. Where should the query state live, and how does SearchBox change it?
Show answer
In SearchPage, their closest common parent. SearchPage passes query down to both children as a prop. It also passes a callback such as onQueryChange to SearchBox, which calls it from its input's onChange handler. SearchPage then calls setQuery, and both children re-render with the new value.