Passing Props to a Component·续
```js src/utils.js export function getImageUrl(imageId, size = 's') { return ( 'https://react.dev/images/docs/scientists/' + imageId + size + '.jpg' ); }
```css
.avatar { margin: 5px; border-radius: 50%; min-height: 70px; }
.profile {
border: 1px solid #aaa;
border-radius: 6px;
margin-top: 20px;
padding: 10px;
}
h1, h2 { margin: 5px; }
h1 { margin-bottom: 10px; }
ul { padding: 0px 10px 0px 20px; }
li { margin: 5px; }
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.
```js src/App.js import { getImageUrl } from './utils.js';
function Profile({ imageId, name, profession, awards, discovery, imageSize = 70 }) { return (
{name}
- Profession: {profession}
- Awards: {awards.length} ({awards.join(', ')})
- Discovered: {discovery}
export default function Gallery() { return (
Notable Scientists
```js src/utils.js
export function getImageUrl(imageId, size = 's') {
return (
'https://react.dev/images/docs/scientists/' +
imageId +
size +
'.jpg'
);
}
.avatar { margin: 5px; border-radius: 50%; min-height: 70px; }
.profile {
border: 1px solid #aaa;
border-radius: 6px;
margin-top: 20px;
padding: 10px;
}
h1, h2 { margin: 5px; }
h1 { margin-bottom: 10px; }
ul { padding: 0px 10px 0px 20px; }
li { margin: 5px; }
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:
```js src/App.js import { getImageUrl } from './utils.js';
function Profile({ person, imageSize = 70 }) { const imageSrc = getImageUrl(person)
return (
{person.name}
- Profession: {person.profession}
- Awards: {person.awards.length} ({person.awards.join(', ')})
- Discovered: {person.discovery}
export default function Gallery() { return (
Notable Scientists
```js src/utils.js
export function getImageUrl(person, size = 's') {
return (
'https://react.dev/images/docs/scientists/' +
person.imageId +
size +
'.jpg'
);
}
.avatar { margin: 5px; border-radius: 50%; min-height: 70px; }
.profile {
border: 1px solid #aaa;
border-radius: 6px;
margin-top: 20px;
padding: 10px;
}
h1, h2 { margin: 5px; }
h1 { margin-bottom: 10px; }
ul { padding: 0px 10px 0px 20px; }
li { margin: 5px; }
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.
```js src/App.js import { getImageUrl } from './utils.js';
function Avatar({ person, size }) {
return (
);
}
export default function Profile() {
return (
```js src/utils.js
export function getImageUrl(person, size) {
return (
'https://react.dev/images/docs/scientists/' +
person.imageId +
size +
'.jpg'
);
}
.avatar { margin: 20px; border-radius: 50%; }
Here is how you could go about it:
```js src/App.js import { getImageUrl } from './utils.js';
function Avatar({ person, size }) {
let thumbnailSize = 's';
if (size > 90) {
thumbnailSize = 'b';
}
return (
);
}
export default function Profile() {
return (
<>
```js src/utils.js
export function getImageUrl(person, size) {
return (
'https://react.dev/images/docs/scientists/' +
person.imageId +
size +
'.jpg'
);
}
.avatar { margin: 20px; border-radius: 50%; }
You could also show a sharper image for high DPI screens by taking window.devicePixelRatio into account:
```js src/App.js import { getImageUrl } from './utils.js';
const ratio = window.devicePixelRatio;
function Avatar({ person, size }) {
let thumbnailSize = 's';
if (size * ratio > 90) {
thumbnailSize = 'b';
}
return (
);
}
export default function Profile() {
return (
<>
```js src/utils.js
export function getImageUrl(person, size) {
return (
'https://react.dev/images/docs/scientists/' +
person.imageId +
size +
'.jpg'
);
}
.avatar { margin: 20px; border-radius: 50%; }
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:
export default function Profile() {
return (
<div>
<div className="card">
<div className="card-content">
<h1>Photo</h1>
<img
className="avatar"
src="/uploads/projects/react-dev-guide/202611/18e72a8855db874e.jpg"
alt="Aklilu Lemma"
width={70}
height={70}
/>
</div>
</div>
<div className="card">
<div className="card-content">
<h1>About</h1>
<p>Aklilu Lemma was a distinguished Ethiopian scientist who discovered a natural treatment to schistosomiasis.</p>
</div>
</div>
</div>
);
}
.card {
width: fit-content;
margin: 20px;
padding: 20px;
border: 1px solid #aaa;
border-radius: 20px;
background: #fff;
}
.card-content {
text-align: center;
}
.avatar {
margin: 10px;
border-radius: 50%;
}
h1 {
margin: 5px;
padding: 0;
font-size: 24px;
}
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:
function Card({ children }) {
return (
<div className="card">
<div className="card-content">
{children}
</div>
</div>
);
}
export default function Profile() {
return (
<div>
<Card>
<h1>Photo</h1>
<img
className="avatar"
src="/uploads/projects/react-dev-guide/202611/18e72a8855db874e.jpg"
alt="Aklilu Lemma"
width={100}
height={100}
/>
</Card>
<Card>
<h1>About</h1>
<p>Aklilu Lemma was a distinguished Ethiopian scientist who discovered a natural treatment to schistosomiasis.</p>
</Card>
</div>
);
}
.card {
width: fit-content;
margin: 20px;
padding: 20px;
border: 1px solid #aaa;
border-radius: 20px;
background: #fff;
}
.card-content {
text-align: center;
}
.avatar {
margin: 10px;
border-radius: 50%;
}
h1 {
margin: 5px;
padding: 0;
font-size: 24px;
}
You can also make title a separate prop if you want every Card to always have a title:
function Card({ children, title }) {
return (
<div className="card">
<div className="card-content">
<h1>{title}</h1>
{children}
</div>
</div>
);
}
export default function Profile() {
return (
<div>
<Card title="Photo">
<img
className="avatar"
src="/uploads/projects/react-dev-guide/202611/18e72a8855db874e.jpg"
alt="Aklilu Lemma"
width={100}
height={100}
/>
</Card>
<Card title="About">
<p>Aklilu Lemma was a distinguished Ethiopian scientist who discovered a natural treatment to schistosomiasis.</p>
</Card>
</div>
);
}
.card {
width: fit-content;
margin: 20px;
padding: 20px;
border: 1px solid #aaa;
border-radius: 20px;
background: #fff;
}
.card-content {
text-align: center;
}
.avatar {
margin: 10px;
border-radius: 50%;
}
h1 {
margin: 5px;
padding: 0;
font-size: 24px;
}
