1. 친숙한 props
2. 컴포넌트에 props 전달하기
3. prop의 기본값 지정하기
4. JSX 전개 구문으로 props 전달하기
5. 자식을 JSX로 전달하기
6. 시간에 따라 props가 변하는 방식
KeyNote
className, src, alt, width, height
는 <img>
태그에 전달할 수 있다.// 예시
function Avatar() {
return (
<img
className="avatar"
src="https://i.imgur.com/1bX5QH6.jpg"
alt="Lin Lanying"
width={100}
height={100}
/>
);
}
export default function Profile() {
return (
<Avatar />
);
}
<img>
태그에 전달할 수 있는 props는 미리 정의되어 있다. (ReactDOM는 HTML 표준을 준수) <Avatar>
와 같은 어떤 component든 props를 전달할 수 있다.2-1. 자식 컴포넌트에 props 전달하기
Avatar
에 몇몇 props를 전달한다.person
(객체)와 size
(숫자)를 전달export default function Profile() {
return (
<Avatar
person={{ name: 'Lin Lanying', imageId: '1bX5QH6' }}
size={100}
/>
);
}
KeyNote
- 만약
person=
뒤에 있는 이중 괄호가 혼란스럽다면, JSX 중괄호 안의 객체라고 기억하면 된다.
2-2. 자식 컴포넌트 내부에서 props 읽기
function Avatar
바로 뒤에 있는 ({와 }) 안에 그들의 이름인 person
, size
등을 쉼표로 구분함으로써 읽을 수 있다.Avatar
코드 내에서 변수를 사용하는 것처럼 사용할 수 있습니다.function Avatar({ person, size }) {
// person and size are available here
}
Avatar
에 렌더링을 위해 person
과 size
props를 사용하는 로직을 추가하면 된다.Avatar
를 다른 props를 이용해 다양한 방식으로 렌더링하도록 구성할 수 있다.// App.js
import { getImageUrl } from './utils.js';
function Avatar({ person, size }) {
return (
<img
className="avatar"
src={getImageUrl(person)}
alt={person.name}
width={size}
height={size}
/>
);
}
export default function Profile() {
return (
<div>
<Avatar
size={100}
person={{
name: 'Katsuko Saruhashi',
imageId: 'YfeOqp2'
}}
/>
<Avatar
size={80}
person={{
name: 'Aklilu Lemma',
imageId: 'OKS67lh'
}}
/>
<Avatar
size={50}
person={{
name: 'Lin Lanying',
imageId: '1bX5QH6'
}}
/>
</div>
);
}
// utils.js
export function getImageUrl(person, size = 's') {
return (
'https://i.imgur.com/' +
person.imageId +
size +
'.jpg'
);
}
Avatar
가 props들을 어떻게 사용하는지 생각할 필요없이 Profile
의 person
또는 size
props를 수정할 수 있다.Profile
을 보지 않고도 Avatar
가 props를 사용하는 방식을 바꿀 수 있다.주의
- props를 선언할 때 ( 및 ) 안에 { 및 } 쌍을 잊지 말자.
- 이 구문을 “구조 분해 할당”이라고 부르며 함수 매개 변수의 속성과 동등하다.
// 예시
function Avatar({ person, size = 100 }) {
// ...
}
<Avatar person={...} />
가 size
prop이 없이 렌더링된다면, size
는 100으로 설정된다. size
prop이 없거나 size={undefined}
로 전달될 때 사용된다.size={null}
또는 size={0}
으로 전달된다면, 기본값은 사용되지 않는다.function Profile({ person, size, isSepia, thickBorder }) {
return (
<div className="card">
<Avatar
person={person}
size={size}
isSepia={isSepia}
thickBorder={thickBorder}
/>
</div>
);
}
Profile
컴포넌트가 Avatar
컴포넌트에게 그런 것처럼, 일부 컴포넌트는 그들의 모든 props를 자식 컴포넌트에 전달한다.Profile
컴포넌트는 props를 직접적으로 사용하지 않기 때문에, 보다 간결한 “전개(spread)” 구문을 사용하는 것이 합리적일 수 있다.function Profile(props) {
return (
<div className="card">
<Avatar {...props} />
</div>
);
}
Profile
의 모든 props를 각각의 이름을 나열하지 않고 Avatar
로 전달합니다.children
이라는 prop으로 받을 것이다.children
prop을 가지고 있는 컴포넌트는 부모 컴포넌트가 임의의 JSX로 “채울” 수 있는 “구멍” 을 가진 것이라고 생각할 수 있다.children
prop를 사용합니다.color
와 time
이라는 두 가지 props를 받는다. (부모 컴포넌트의 코드는 아직 자세히 다루지 않을 state를 사용하기 때문에 생략)// Clock.js
export default function Clock({ color, time }) {
return (
<h1 style={{ color: color }}>
{time}
</h1>
);
}
color
prop은 다른 색상을 선택하면 변경된다.function Avatar({ person, size })
구조 분해 구문을 사용한다.size = 100
과 같은 기본값을 지정할 수 있으며, 이는 누락되거나 undefined
인 props에 사용된다.<Avatar {...props} />
JSX 전개 구문을 사용할 수 있지만, 과도하게 사용하지 말자.<Card><Avatar /></Card>
와 같이 중첩된 JSX는 Card컴포넌트의 children
prop로 표시된다.