โ๐ป Props
props๋?
- ์ปดํฌ๋ํธ์ ์์ฑ(property)์ ์๋ฏธํ๋ค
- ์ฑ๋ณ์ด๋ ์ด๋ฆ์ฒ๋ผ ๋ณํ์ง ์๋ ์ธ๋ถ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ ๊ฐ, ์น ์ดํ๋ฆฌ์ผ์ด์
์์ ํด๋น ์ปดํฌ๋ํธ๊ฐ ๊ฐ์ง ์์ฑ
- ์์ ์ปดํฌ๋ํธ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ ๊ฐ
- props๋ฅผ ํจ์์ ์ ๋ฌ์ธ์(atguments)์ฒ๋ผ ์ ๋ฌ
- React ์๋ฆฌ๋จผํธ ๋ฐํ
- ์ปดํฌ๋ํธ๊ฐ ์ต์ด ๋ ๋๋ง๋ ๋์ ์ถ๋ ฅํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ด๊ธฐ๊ฐ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
- ๊ฐ์ฒด ํํ
- ์ด๋ค ํ์
์ ๊ฐ๋ ๋ฃ์ด ์ ๋ฌ ๊ฐ๋ฅ
- props๋ ์ฝ๊ธฐ ์ ์ฉ(read-only)
- ์ธ๋ถ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ ๋ณํ์ง ์๋ ๊ฐ(side-effect ์๋ฐฉ)
props ์ฌ์ฉ ๋ฐฉ๋ฒ 3๋จ๊ณ
- ํ์ ์ปดํฌ๋ํธ์ ์ ๋ฌํ๊ณ ์ ํ๋ ๊ฐ๊ณผ ์์ฑ์ ์ ์
<Parent>
,<Child>
์ปดํฌ๋ํธ ์ ์ธ
function Parent() {
return (
<div className="parent">
<h1>parent</h1>
<Child />
</div>
);
};
function Child() {
return (
<div className="child"></div>
);
};
- props๋ฅผ ์ด์ฉํ์ฌ ์ ์ ๋ ๊ฐ๊ณผ ์์ฑ ์ ๋ฌ
React์์ JSX ์์ฑ ๋ฐ ๊ฐ ํ ๋น ๋ฐฉ๋ฒ
<Child attribute={value} />
<Child text={"I'm the eldest child"} />
- ์ ๋ฌ๋ฐ์ props ๋ ๋๋ง
props๋ ๊ฐ์ฒด์ด๋ฏ๋ก ๊ฐ์ฒด์ { key : value}
๋ก ๋ฐํํ๋ค.
<Parent>
์ปดํฌ๋ํธ์์ ์ ์ํ { attribute : value}
๋ก ๋ฐํํ๋ค.
function Child(props) {
return (
<div className="child"></div>
<p>{props.text}</p>
);
};
๊ทธ ์ธ props.children ๋ฐฉ๋ฒ
ํ๊ทธ ์ฌ์ด์ value๋ฅผ ๋ฃ์ด ์ ๋ฌ
function Parent() {
return (
<div className="parent">
<h1>parent</h1>
<Child>I'm the child</Child>
</div>
);
};
function Child(props) {
return (
<div className="child">
<p>{props.children}</p>
</div>
);
};