
React์์ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ์์ ์ปดํฌ๋ํธ์๊ฒ ์ ๋ฌํ ์ ์๋ ํน๋ณํ prop์
๋๋ค.
์ด๋ฅผ ํตํด ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ ์์ ์ปดํฌ๋ํธ์ ํ๊ทธ ์ฌ์ด์ ์์นํ ๋ชจ๋ JSX๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ค์ ํฌํจํ ์ ์์ต๋๋ค.
๋ค์ ๋งํด์, props.children์ ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์คํ๋ ํ๊ทธ์ ํด๋ก์ง ํ๊ทธ ์ฌ์ด์ ์ฝ์
๋ ๋ชจ๋ ๊ฒ์ ์๋์ผ๋ก ๋ฐ์์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ prop์
๋๋ค.
function Card({ children }) {
return <div className="card">{children}</div>;
}
function App() {
return (
<Card>
<h1>Hello, World!</h1>
<p>This is inside the card.</p>
</Card>
);
}
์ฌ๊ธฐ์ Card ์ปดํฌ๋ํธ๋ {children}์ ์ฌ์ฉํ์ฌ ๋ด๋ถ์ ํฌํจ๋ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅํฉ๋๋ค. App ์ปดํฌ๋ํธ์์ Card ์ปดํฌ๋ํธ ์ฌ์ด์ ์์นํ <h1>๊ณผ <p> ํ๊ทธ๋ Card ์ปดํฌ๋ํธ์ children์ผ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
๋ชจ๋ฌ(dialog) ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋ props.children์ ์ ์ฉ์ฑ์ ๋ณผ ์ ์์ต๋๋ค.
function Modal({ children }) {
return (
<div className="modal-background">
<div className="modal-content">{children}</div>
</div>
);
}
function App() {
return (
<Modal>
<h2>Confirm Your Action</h2>
<p>Are you sure you want to delete this item?</p>
<button>Confirm</button>
</Modal>
);
}
์ฌ๊ธฐ์ Modal ์ปดํฌ๋ํธ๋ ํค๋, ๋ด์ฉ, ๋ฒํผ ๋ฑ ์ด๋ ํ ๋ด์ฉ์ด๋ ํฌํจํ ์ ์์ต๋๋ค. ์ด๋ฌํ ์ ์ฐ์ฑ์ props.children ๋๋ถ์ ๊ฐ๋ฅํฉ๋๋ค.
props.children์ ์ฌ์ฉํ๋ฉด ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋ ๊ทธ ๋ด๋ถ์ ๋ค์ด๊ฐ ๋ด์ฉ์ ์์ธกํ์ง ์๊ณ ๋ ๋ฒ์ฉ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ ์ปดํฌ๋ํธ์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , ์ฝ๋์ ์ค๋ณต์ ์ค์ด๋ฉฐ, ๋ค์ํ ์ํฉ์์ ๋์ผํ ์ปดํฌ๋ํธ๋ฅผ ํ์ฉํ ์ ์๊ฒ ํด ์ค๋๋ค.
function Modal({ header, body, footer }) {
return (
<div className="modal-background">
<div className="modal-header">{header}</div>
<div className="modal-body">{body}</div>
<div className="modal-footer">{footer}</div>
</div>
);
}
function App() {
return (
<Modal
header={<h2>Confirm Your Action</h2>}
body={<p>Are you sure you want to delete this item?</p>}
footer={<button>Confirm</button>}
/>
);
}
