Sub topic 7
Passing data with props
This chapter assumes you can write a function component that returns JSX, and that you know JavaScript destructuring and ES modules. It builds on embedding expressions in JSX with curly braces.
The working model
Props (short for properties) are the inputs to a component. When you write attributes on a component tag, React collects them into a single object and passes it as the function's first argument. Data flows one way: from a parent component down to its child.
Here is the smallest complete example:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
export default function App() {
return <Greeting name="Ada" />;
}<Greeting name="Ada" /> makes React call Greeting({ name: "Ada" }). The parameter { name } destructures that object. You could also write function Greeting(props) and read props.name. Both forms are equivalent, and destructuring makes the component's inputs visible in its signature.
Passing values that are not strings
A quoted attribute always produces a string. To pass any other value, use curly braces, which hold a JavaScript expression.
function Badge({ label, count, active, onSelect }) {
return (
<button onClick={onSelect} disabled={!active}>
{label}: {count}
</button>
);
}
export default function App() {
return (
<Badge
label="Unread"
count={3}
active={true}
onSelect={() => console.log('selected')}
/>
);
}Here count={3} is a number. count="3" would be a string, which causes subtle bugs once you do arithmetic or strict comparisons. Functions can be props too, which is how a child tells its parent that something happened.
An attribute with no value, such as <Badge active />, is shorthand for active={true}.
Default values
Give a prop a default with destructuring syntax. The default applies only when the prop is undefined.
function Avatar({ src, size = 48 }) {
return <img src={src} width={size} height={size} alt="" />;
}<Avatar src="/ada.png" /> renders a 48-pixel image. Passing size={null} does not trigger the default, because null is not undefined.
Forwarding many props
The spread operator passes every property of an object as a prop:
function Profile(props) {
return <Avatar {...props} />;
}This is convenient for wrappers. Use it sparingly, because it hides which props a component actually needs.
Props are read-only
A component must never change its own props. Treat the props object as frozen.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Cannot read properties of undefined` | The parent did not pass the prop, or you misspelled its name | Check the attribute name in the parent. Add a default if the prop is optional. |
| A number behaves like text | Passed `count="3"` instead of `count={3}` | Use curly braces. |
| The whole component shows `[object Object]` | Rendered an object directly, such as `{user}` | Render a field, such as `{user.name}`. |
| The handler runs during render | Wrote `onSelect={handle()}` | Pass the function itself: `onSelect={handle}`. |
| A prop is `undefined` in the child | Destructured with the wrong shape, such as `function Greeting(name)` | Use `{ name }` or `props.name`. |
The second to last row is worth remembering. Components receive one object, so function Greeting(name) binds name to the entire props object.
Exercise
Write a component Price that takes amount (a number) and currency (default "USD") and renders <span> text like 19.99 USD. Show how a parent renders it with an amount of 19.99 and no currency.
Show answer
function Price({ amount, currency = 'USD' }) { return <span>{amount} {currency}</span>; } The parent writes <Price amount={19.99} />. The amount uses curly braces so it is a number, and the missing currency prop is undefined, so the default "USD" applies.
Exercise
A child component contains props.items.push('new'). Why is this a bug, and where should the change happen instead?
Show answer
Props are read-only, so the child must not mutate data it received. The mutation changes the parent's array without React knowing, so the screen may not update. The parent that owns the data should change it, typically through state, and pass a new array down.