부모 컴포넌트가 자식 컴포넌트에게 물려준 데이터 즉 컴포넌트 간의 정보 교류 방법
// src/App.jsx
import React from "react";
function App() {
return <GrandFather />;
}
function GrandFather() {
return <Mother />;
}
function Mother() {
const name = '홍부인';
return <Child />;
}
function Child() {
return <div>연결 성공</div>;
}
export default App;
// src/App.jsx
import React from "react";
function App() {
return <GrandFather />;
}
function GrandFather() {
return <Mother />;
}
function Mother() {
const name = '홍부인';
return <Child motherName={name} />; // 💡"props로 name을 전달했다."
}
function Child() {
return <div>연결 성공</div>;
}
export default App;
그렇다면, Mother가 전달해준 motherName은 Child가 어떻게 받을 수 있을까?
function Child(props){
console.log(props) // 이게 바로 props다.
return <div>연결 성공</div>
}
import React from "react";
// div안에서 { } 를 쓰고 props.motherName을 넣어보세요.
function Child(props) {
return <div>{props.motherName}</div>;
}
function Mother() {
const name = "홍부인";
return <Child motherName={name} />;
}
function GrandFather() {
return <Mother />;
}
function App() {
return <GrandFather />;
}
export default App;