Sub topic 5
Passing data with props
This chapter assumes you know JavaScript objects and destructuring, and that you have seen a basic function component and JSX. You do not need a running project to follow along, though the examples work in the Vite app from the earlier chapters.
What props are
Props (short for properties) are the inputs to a component. A parent component passes them in using attribute syntax, and the child receives them as a single object in its first argument. Data flows one way: from parent to child.
function Greeting({ name, excited }) {
return <p>Hello, {name}{excited ? '!' : '.'}</p>
}
function App() {
return (
<main>
<Greeting name="Ada" excited />
<Greeting name="Grace" excited={false} />
</main>
)
}
export default AppGreeting could also be written as function Greeting(props) and read props.name. Destructuring in the parameter list is the common style because it shows at a glance which props the component expects.
Passing values of different types
String props can use quotes. Everything else goes inside curly braces, because the braces switch from markup back to JavaScript.
function ProfileCard({ name, age, skills, onSelect }) {
return (
<section>
<h2>{name} ({age})</h2>
<ul>
{skills.map((skill) => (
<li key={skill}>{skill}</li>
))}
</ul>
<button onClick={() => onSelect(name)}>Select</button>
</section>
)
}
export default function App() {
return (
<ProfileCard
name="Ada"
age={36}
skills={['math', 'code']}
onSelect={(name) => console.log(`Selected ${name}`)}
/>
)
}A prop can be a number, array, object, or function. Passing a function, as onSelect does, is how a child tells its parent that something happened.
Default values and children
Give a prop a default in the destructuring pattern. The default applies only when the prop is undefined.
Anything you place between a component's opening and closing tags arrives as the special children prop.
function Panel({ title = 'Untitled', children }) {
return (
<section>
<h2>{title}</h2>
{children}
</section>
)
}
export default function App() {
return (
<Panel title="Notes">
<p>Props make components reusable.</p>
</Panel>
)
}Props are read-only
A component must never change its own props. Assigning to props.name is a bug, and React treats props as immutable. If a value needs to change over time, it belongs in state, which the previous chapter covered with useState. A parent can keep the state and pass the current value down as a prop.
Common errors
| Symptom | Likely cause |
|---|---|
| `age` renders as the text `"36"` and math fails | You wrote `age="36"` instead of `age={36}` |
| `Cannot read properties of undefined` | A prop was not passed, and the component uses it without a default |
| The prop is ignored | The name differs in case, such as `OnSelect` and `onSelect` |
| Extra braces appear, or the object is printed as an error | You passed an object as a child, for example `{user}` instead of `{user.name}` |
Exercise
A parent renders <Badge count="5" />. Inside Badge, count + 1 shows 51. What is wrong, and how do you fix it?
Show answer
The attribute count="5" passes the string "5", so + concatenates. Pass a number with count={5}.
Exercise
A child needs to tell its parent that a button was clicked. How do you do this when data only flows downward?
Show answer
The parent passes a function as a prop, such as onSelect. The child calls that function, and the parent's code runs.