Sub topic 1
Quick refresher: props and one-way data flow
Prerequisites
This chapter assumes you can write JavaScript functions, use destructuring and arrow functions, and read basic JSX. You should have built a function component and used the useState hook at least once. If you have, this refresher will be quick. It sets up the vocabulary that the rest of the tutorial on lifting state up relies on.
What props are
Props are the inputs to a component. A parent passes them as attributes in JSX, and the child receives them as a single object in its first argument. Destructuring in the parameter list is the usual way to unpack them.
function Greeting({ name, excited }) {
return <p>Hello, {name}{excited ? '!' : '.'}</p>;
}
export default function App() {
return (
<>
<Greeting name="Ada" excited={true} />
<Greeting name="Grace" excited={false} />
</>
);
}Props can hold any JavaScript value: strings, numbers, objects, arrays, and functions. Strings can be written as plain attribute text. Everything else goes inside curly braces.
One-way data flow
Data in React moves in one direction. A parent passes values down to its children, and children never pass values back up through props. This is called one-way data flow, or top-down data flow.
This gives you a simple rule for debugging. If a component shows a wrong value, the value came from its props or from its own state. You can follow the props upward until you find the component that owns the value.
Props are read-only
A component must never change its own props. Treat them as frozen for the duration of a render. Assigning to a prop is a bug, even when JavaScript lets you do it for an object's fields.
function Badge({ count }) {
// Wrong: props are inputs, not variables you own.
// count = count + 1;
// Right: compute a new value and leave the prop alone.
const next = count + 1;
return <span>{next}</span>;
}If a value must change over time, it is not a prop in the component that changes it. It is state, and some component has to own it. The next chapter covers that distinction. For now, remember that a child can display a prop but cannot update it.
A child cannot reach up
Because data only flows down, a child has no direct way to change a value held by its parent. Suppose Counter receives count as a prop. It cannot write a new count back to App.
function Counter({ count }) {
return <button>Clicked {count} times</button>;
}
export default function App() {
const count = 0;
return <Counter count={count} />;
}Clicking this button does nothing, because count is a plain constant and nothing can change it. This limit shapes everything that follows in this tutorial. When a child needs to request a change, the parent must hand it a function, and the child calls that function. That pattern comes later in this tutorial.
Props versus state
The two are easy to confuse, so compare them directly.
| Row | Props | State |
|---|---|---|
| Owned by | The parent that passes them | The component that declares it |
| Can the component change it? | No, read-only | Yes, through the setter function |
| Where it comes from | Attributes in JSX | `useState` |
| Updates when | The parent re-renders with new values | The setter is called |
When a parent re-renders and passes a new value, the child re-renders with the new prop. That is the only way a prop changes.
Common mistakes at this stage
- Writing
value="5"when you meantvalue={5}. The first is a string and the second is a number, which matters later when you compare or convert values. - Mutating an object or array received as a prop. Copy it first, then change the copy.
- Copying a prop into state with
useState(props.value). The state starts from the prop and then ignores later changes to it. This leads to duplicated state, which a later chapter addresses.
Exercise
A child component receives a title prop. A teammate writes title = title.toUpperCase(); inside the child to display it in capitals. What is wrong, and what is a better way?
Show answer
Props are read-only, so the child should not reassign title. Compute a new value instead, for example const shown = title.toUpperCase();, and render shown. The prop stays unchanged and the parent remains the owner of the original value.
Exercise
In a tree where App renders Content and Content renders Item, can Item send a value directly to App through props? How would the data have to travel?
Show answer
No. Props only flow downward. App can pass a value to Content, and Content can pass it on to Item. For Item to affect App, App would have to pass down a function that Item calls.