React์์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๋ค ๋ณด๋ฉด Props์ ํ์
์ ์ด๋ป๊ฒ ์ ์ํ ์ง ๊ณ ๋ฏผํ ๋๊ฐ ๋ง๋ค.
interface
, type
, React.FC
์ด๋ ๊ฒ 3๊ฐ์ง๊ฐ ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ธ๋ฐ,
๊ฐ๊ฐ์ ํน์ง๊ณผ ์ธ์ ์ด๋ค ๊ฑธ ์ฐ๋ ๊ฒ ์ข์์ง ์ ๋ฆฌํด๋ดค๋ค.
interface
์ฌ์ฉinterface ButtonProps {
label: string;
onClick: () => void;
}
function Button({ label, onClick }: ButtonProps) {
return <button onClick={onClick}>{label}</button>;
}
extends
)type
์ฌ์ฉtype ButtonProps = {
label: string;
onClick: () => void;
};
interface
๋ณด๋ค ์กฐ๊ธ ๋ ์์ ๋กญ๊ณ ๊ฐ๋ ฅํ ๋๋React.FC
์ฌ์ฉinterface ButtonProps {
label: string;
}
const Button: React.FC<ButtonProps> = ({ label }) => {
return <button>{label}</button>;
};
children
์ด ์๋ ํฌํจ๋จprops
์ ReactNode
๊ฐ ํฌํจ๋ผ ์์children
์ด ํ์๊ฐ ์๋๋ฐ ์๋์ผ๋ก ํฌํจ๋ผ์ ๋ฒ๊ทธ ์์ธ์ด ๋๊ธฐ๋ ํจ๊ธฐ์ค | interface | type | React.FC |
---|---|---|---|
ํ์ฅ์ฑ | โ ์ข์ | ๐ ์ ๋์จ/์กฐํฉ ๊ฐํจ | โ ๋ณต์กํ ํ์ ์ ๋ถํธํจ |
children ์๋ ํฌํจ | โ ์์ | โ ์์ | โ ์๋ ํฌํจ๋จ |
๊ฐ๋ ์ฑ/๋ช ์์ฑ | โ ๋ช ํํจ | โ ์์ ๋ก์ | โ ์๋ฌต์ ์ผ๋ก ๋์ํจ |
์์ฆ ์ถ์ธ | โ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ | โ ์ํฉ ๋ฐ๋ผ ์ฌ์ฉ | โ ์ง์ํ๋ ์ถ์ธ |
interface
๋ ๊ฑฐ์ ๊ธฐ๋ณธ๊ฐ์ฒ๋ผ ์ฌ์ฉ (ํนํ props ์ ์ ์)type
์ ์ ๋์จ ํ์
์ด๋, ๋ด๋ถ์ ์ผ๋ก ์กฐํฉ์ด ๋ง์ ๋React.FC
๋ ์ฐ์ง ์์ (children์ ๋ช
์์ ์ผ๋ก ๋ฐ๊ณ ์ถ์)์ฒ์์ React.FC
๋ฅผ ๋ง์ด ๋ด์ ์์ฐ์ค๋ฝ๊ฒ ์ผ์๋๋ฐ,
์ง๊ธ์ ์คํ๋ ค ๋ช
์์ ์ผ๋ก props๋ฅผ ์ค๊ณํ๋ ๊ฒ ํ์
์์๋ ํจ์ฌ ๋ซ๋ค๋ ๊ฑธ ๋๋ผ๊ณ ์๋ค.
ํนํ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ธ์๋ก ์ฝ๋๋ฅผ ๋ ๋ช
ํํ๊ฒ ์์ฑํ๊ณ ์ถ์ด์ง๋๋ผ.
โ๏ธ "ํ์ ์ ์ ์ํ๋ ๊ฑด ๋จ์ํ ๋ฌธ๋ฒ์ด ์๋๋ผ, ํ์ ์ ์ฝ์์ด๋ค."