Passing Props to a Component 🚧
Overview¶
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them props. Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions.
You will learn
- How to pass props to a component
- How to read props from a component
- How to specify default values for props
- How to pass some JSX to a component
- How props change over time
Familiar props¶
Props are the information that you pass to a JSX tag. For example, className
, src
, alt
, width
, and height
are some of the props you can pass to an <img>
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 |
|
The props you can pass to an <img>
tag are predefined (ReactDOM conforms to the HTML standard). But you can pass any props to your own components, such as <Avatar>
, to customize them. Here's how!
Passing props to a component¶
In this code, the Profile
component isn't passing any props to its child component, Avatar
:
1 2 3 |
|
You can give Avatar
some props in two steps.
Step 1: Pass props to the child component¶
First, pass some props to Avatar
. For example, let's pass two props: person
(an object), and size
(a number):
1 2 3 4 5 6 7 8 |
|
If double curly braces after person=
confuse you, recall they're merely an object inside the JSX curlies.
Now you can read these props inside the Avatar
component.
Step 2: Read props inside the child component¶
You can read these props by listing their names person, size
separated by the commas inside ({
and })
directly after function Avatar
. This lets you use them inside the Avatar
code, like you would with a variable.
1 2 3 |
|
Add some logic to Avatar
that uses the person
and size
props for rendering, and you're done.
Now you can configure Avatar
to render in many different ways with different props. Try tweaking the values!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
1 2 3 |
|
1 2 3 4 5 6 7 |
|
Props let you think about parent and child components independently. For example, you can change the person
or the size
props inside Profile
without having to think about how Avatar
uses them. Similarly, you can change how the Avatar
uses these props, without looking at the Profile
.
You can think of props like "knobs" that you can adjust. They serve the same role as arguments serve for functions—in fact, props are the only argument to your component! React component functions accept a single argument, a props
object:
1 2 3 4 5 |
|
Usually you don't need the whole props
object itself, so you destructure it into individual props.
Don't miss the pair of {
and }
curlies inside of (
and )
when declaring props:
1 2 3 |
|
This syntax is called "destructuring" and is equivalent to reading properties from a function parameter:
1 2 3 4 5 |
|
Specifying a default value for a prop¶
If you want to give a prop a default value to fall back on when no value is specified, you can do it with the destructuring by putting =
and the default value right after the parameter:
1 2 3 |
|
Now, if <Avatar person={...} />
is rendered with no size
prop, the size
will be set to 100
.
The default value is only used if the size
prop is missing or if you pass size={undefined}
. But if you pass size={null}
or size={0}
, the default value will not be used.
Forwarding props with the JSX spread syntax¶
Sometimes, passing props gets very repetitive:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
There's nothing wrong with repetitive code—it can be more legible. But at times you may value conciseness. Some components forward all of their props to their children, like how this Profile
does with Avatar
. Because they don't use any of their props directly, it can make sense to use a more concise "spread" syntax:
1 2 3 4 5 6 7 |
|
This forwards all of Profile
's props to the Avatar
without listing each of their names.
Use spread syntax with restraint. If you're using it in every other component, something is wrong. Often, it indicates that you should split your components and pass children as JSX. More on that next!
Passing JSX as children¶
It is common to nest built-in browser tags:
1 2 3 |
|
Sometimes you'll want to nest your own components the same way:
1 2 3 |
|
When you nest content inside a JSX tag, the parent component will receive that content in a prop called children
. For example, the Card
component below will receive a children
prop set to <Avatar />
and render it in a wrapper div:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Try replacing the <Avatar>
inside <Card>
with some text to see how the Card
component can wrap any nested content. It doesn't need to "know" what's being rendered inside of it. You will see this flexible pattern in many places.
You can think of a component with a children
prop as having a "hole" that can be "filled in" by its parent components with arbitrary JSX. You will often use the children
prop for visual wrappers: panels, grids, etc.
How props change over time¶
The Clock
component below receives two props from its parent component: color
and time
. (The parent component's code is omitted because it uses state, which we won't dive into just yet.)
Try changing the color in the select box below:
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
This example illustrates that a component may receive different props over time. Props are not always static! Here, the time
prop changes every second, and the color
prop changes when you select another color. Props reflect a component's data at any point in time, rather than only in the beginning.
However, props are immutable—a term from computer science meaning "unchangeable". When a component needs to change its props (for example, in response to a user interaction or new data), it will have to "ask" its parent component to pass it different props—a new object! Its old props will then be cast aside, and eventually the JavaScript engine will reclaim the memory taken by them.
Don't try to "change props". When you need to respond to the user input (like changing the selected color), you will need to "set state", which you can learn about in State: A Component's Memory.
- To pass props, add them to the JSX, just like you would with HTML attributes.
- To read props, use the
function Avatar({ person, size })
destructuring syntax. - You can specify a default value like
size = 100
, which is used for missing andundefined
props. - You can forward all props with
<Avatar {...props} />
JSX spread syntax, but don't overuse it! - Nested JSX like
<Card><Avatar /></Card>
will appear asCard
component'schildren
prop. - Props are read-only snapshots in time: every render receives a new version of props.
- You can't change props. When you need interactivity, you'll need to set state.
Extract a component¶
This Gallery
component contains some very similar markup for two profiles. Extract a Profile
component out of it to reduce the duplication. You'll need to choose what props to pass to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Start by extracting the markup for one of the scientists. Then find the pieces that don't match it in the second example, and make them configurable by props.
In this solution, the Profile
component accepts multiple props: imageId
(a string), name
(a string), profession
(a string), awards
(an array of strings), discovery
(a string), and imageSize
(a number).
Note that the imageSize
prop has a default value, which is why we don't pass it to the component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Note how you don't need a separate awardCount
prop if awards
is an array. Then you can use awards.length
to count the number of awards. Remember that props can take any values, and that includes arrays too!
Another solution, which is more similar to the earlier examples on this page, is to group all information about a person in a single object, and pass that object as one prop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Although the syntax looks slightly different because you're describing properties of a JavaScript object rather than a collection of JSX attributes, these examples are mostly equivalent, and you can pick either approach.
Adjust the image size based on a prop¶
In this example, Avatar
receives a numeric size
prop which determines the <img>
width and height. The size
prop is set to 40
in this example. However, if you open the image in a new tab, you'll notice that the image itself is larger (160
pixels). The real image size is determined by which thumbnail size you're requesting.
Change the Avatar
component to request the closest image size based on the size
prop. Specifically, if the size
is less than 90
, pass 's'
("small") rather than 'b'
("big") to the getImageUrl
function. Verify that your changes work by rendering avatars with different values of the size
prop and opening images in a new tab.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 |
|
1 2 3 4 |
|
Here is how you could go about it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
1 2 3 |
|
1 2 3 4 |
|
You could also show a sharper image for high DPI screens by taking window.devicePixelRatio
into account:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
1 2 3 |
|
1 2 3 4 |
|
Props let you encapsulate logic like this inside the Avatar
component (and change it later if needed) so that everyone can use the <Avatar>
component without thinking about how the images are requested and resized.
Passing JSX in a children
prop¶
Extract a Card
component from the markup below, and use the children
prop to pass different JSX to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Any JSX you put inside of a component's tag will be passed as the children
prop to that component.
This is how you can use the Card
component in both places:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
You can also make title
a separate prop if you want every Card
to always have a title:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|