๐ฌ ReactJS๋ก ์ํ ์น ์๋น์ค ๋ง๋ค๊ธฐ (06) - Props
Props
Props๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ก๋ถํฐ ์์ ์ปดํฌ๋ํธ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ์ ์๊ฒ ํด์ฃผ๋ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ํ ์ ์๋ค!
(์ฌ๊ธฐ์ ๋งํ๋ ์ปดํฌ๋ํธ๋ function(ํจ์)๋ฅผ ์๋ฏธ)
์๋ฅผ ๋ค์ด, ๋ฒํผ ์์ ํ
์คํธ ๋ด์ฉ๋ง ๋ค๋ฅด๊ณ ์คํ์ผ์ ๋๊ฐ์ ๋ฒํผ ๋๊ฐ๋ฅผ ๋ง๋ค๊ณ ์ถ์๋ฐ ๊ทธ๋ด๋๋ง๋ค ๊ฐ๊ฐ์ ๋ฒํผ์ ๋๊ฐ์ ๋ด์ฉ์ ์คํ์ผ ์ฝ๋๋ฅผ ๋ณต๋ถํ๋๊ฑด ๋งค์ฐ ๋นํจ์จ์ .
๊ทธ ๋์ ์ด๋ฐ ์คํ์ผ์ ๋ชจ๋ ๊ฐ๋ ํ๋์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด ํ์ํ ๋๋ง๋ค ์ฌ์ฌ์ฉํ๋ฉด ๋๋ค
๐ฝ ์์ ๐ฝ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Btn() {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
borderRadius: 10,
border: 0
}}
>
Save Changes
</button>
);
}
Btn
์ด๋ผ๋ ํจ์ํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด์ ๋ด๊ฐ ์ํ๋ ์คํ์ผ๋ค์ ์ง์ ํ๊ณ ,
1
2
3
4
5
6
7
8
function App() {
return (
<div>
<Btn banana="Save Changes" />
<Btn banana="Continue" />
</div>
);
}
๋ฒํผ ์์ ํ
์คํธ๋ <Btn text="Save Changes" />
์ด๋ฐ ์์ผ๋ก syntax๋ฅผ ์ถ๊ฐํด ํ
์คํธ๋ฅผ ๊ฐ๊ฐ ๋ค๋ฅด๊ฒ ์
๋ ฅํ ์ ์๋ค. ์ฌ๊ธฐ์ text
๋ ๊ผญ text๊ฐ ์๋์ด๋ ๋๋ค. potato๋ผ๊ณ ์ง๋ , banana๋ผ๊ณ ์ง๋ ๋ด๋งด์
์ฌ๊ธฐ๊น์ง์ ๊ฒฐ๊ณผ๋ฌผ์ ํ์ธํด๋ณด๋ฉด,
๋ฒํผ ์์ ํ
์คํธ ๋ด์ฉ์ด ๋๊ฐ๋ค ๐ค
์๋ํ๋ฉด ๋๋ banana๋ฅผ Btn์๊ฒ ๋ณด๋๋๋ฐ, Btn์ banana๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์๊ธฐ ๋๋ฌธ!
๋ด๊ฐ ๋ง๋ค๊ณ ์ฌ์ฉํ๋ ๋ชจ๋ ์ปดํฌ๋ํธ๋ค์ ์ธ์(argument)๋ฅผ ๋ฐ๋๋ฐ, ์ด ์ธ์์ ์ด๋ฆ ๋ํ ๋ด ๋ง๋๋ก ์ง์ ์ ์์ง๋ง ๋ณดํต ์ฌ๋๋ค์ ์ด๊ฑธ props๋ผ๊ณ ํ๋ค
Btn์ผ๋ก๋ถํฐ ์ ๋ฌ๋ฐ๋ properties
๊ทธ๋ฆฌ๊ณ ์ฝ์๋ก๊ทธ๋ฅผ ์ฐ์ด๋ณด๋ฉด ์๊ฒ ์ง๋ง props
๋ ์ค๋ธ์ ํธ์ด๋ค. ์ฐ๋ฆฌ๊ฐ ๋ฃ์ ๋ชจ๋ ๊ฒ์ ๊ฐ๋ ์ค๋ธ์ ํธ!
์ํผ ๊ทธ๋์ ! banana๋ฅผ Btn์ ์ธ์๋ก ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Btn(props) {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
borderRadius: 10,
border: 0
}}
>
{props.banana}
</button>
);
}
์ด๋ ๊ฒ ! ๊ทธ๋ผ ์ด์
ํ ์คํธ ๋ด์ฉ์ด ๋ฐ์๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Btn({ banana }) {
return (
<button
style={{
backgroundColor: "tomato",
color: "white",
padding: "10px 20px",
borderRadius: 10,
border: 0
}}
>
{banana}
</button>
);
}
์ด๋ ๊ฒ ๋ ์งง๊ฒ ์ ๋ ๋ฐฉ๋ฒ๋ ์๋น