[부모 컨포넌트]
import React from "react";
import CardList from "./Components/CardList/CardList";
function Monsters() {
return (
<div className="component">
<CardList cardName="토끼" />
</div>
)
}
[자식 컨포넌트]
function CardList(props) {
//props에서 무엇을 받아오는지 알고 싶다면 console.log()를 사용해보자!
return <div> 내 이름은 {props.cardName}입니다.</div>
}
// 내 이름은 토끼입니다.
위와 같이 부모 컨퍼넌트에 포함된 자식 컨포넌트CardList
에 cardName="토끼"
props를 설정 해주어 CardList
컨포넌트 내부에서 function CardList(props)
props를 인자로 받아와서 사용할 수 있다.
[자식 컨포넌트]
function CardList(props) {
const [cardName] = props
return <div> 내 이름은 {cardName}입니다.</div>
}
// 내 이름은 토끼입니다.
비구조화 할당을 사용해서 받아온 props를 더 간단하게 사용할 수 있다.
Tip : 아래와 같이 비구조화는 받아오는 인자 내부에서 바로 실행할 수 있다.
[자식 컨포넌트]
function CardList({cardName}) {
return <div> 내 이름은 {cardName}입니다.</div>
}
// 내 이름은 토끼입니다.