Component
ํ์ต ํค์๋
Component
ํด๋์ค ์ปดํฌ๋ํธ์ ํจ์ํ ์ปดํฌ๋ํธ
์ปดํฌ๋ํธ ์์ฑ ๊ท์น
JSX
Props์ ํ์ฉ๋ฒ
Component
๋ฆฌ์กํธ๋ ์น์ ๊ตฌ์ฑ์์๋ฅผ ๊ฐ์ฅ ์์ ๋จ์๋ก ์ธ ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํ๋ค. ์ด๋ฌํ ์ปดํฌ๋ํธ๋ฅผ ์กฐํฉํด์ ํ๋์ ํ์ด์ง๋ก ์์ฑํ๊ณ , ๋ฐ๋ณต์ ์ธ ์น์ ๊ตฌ์ฑ์์๋ฅผ ์ฌ์ฌ์ฉ ํ ์ ์๋ค๋ ํน์ง์ ๊ฐ์ง๊ณ ์๋ค.
ํด๋์ค ์ปดํฌ๋ํธ ์ ํจ์ํ ์ปดํฌ๋ํธ
๋ฆฌ์กํธ๋ ํด๋ํ๊ณผ ํจ์ํ ์ปดํฌ๋ํธ ๋๊ฐ์ง ๋ฐฉ์์ผ๋ก ์์ฑํ ์ ์๋ค. ๋๊ฐ์ง ๋ฐฉ์ ๋ชจ๋ ์ง์ํ๊ณ ์์ง๋ง, ๊ณต์์ ์ผ๋ก๋ ํด๋์ค ์ปดํฌ๋ํธ ๋ฐฉ์์ ์ฌ์ฉํ์ง ์๋๊ฑธ ๊ถ์ฅํ๋ค. ๋ฆฌ์กํธ 16.8๋ฒ์ ์ ์ถ๊ฐ๋ ๋ฆฌ์กํธ ํ
์ผ๋ก ์ธํด ํจ์ํ ์ปดํฌ๋ํธ์ ์ฌ์ฉ์ฑ์ด ๊ฐ์ ๋์ด ๋์ด์ ํด๋์ค ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฑด ๋นํจ์จ์ ์ด๋ค.
์ปดํฌ๋ํธ ์์ฑ ๊ท์น
์ปดํฌ๋ํธ ์์ฑ์ ์ด๋ฆ์ ํญ์ ๋๋ฌธ์๋ก ์์ํ๋ค.
export default function App() {}
ํ์ดํํ์, ํจ์์ ์ธ์ ๋๋ค ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
export default function App() {}
const App = () => {};
export default App; //ํจ์ ํํ์์ผ๋ก ์ฌ์ฉ์ ํด๋น ๋ฐฉ๋ฒ์ผ๋ก ๋ด๋ณด๋ด์ผ ํ๋ค.
JSX
Javascript + XML
๋ฐ๋์ ์ต์์ ๋ฃจํธ ํ๊ทธ๊ฐ ์กด์ฌํด์ผ ํ๋ค
export default function App() {
return (
<>
<div>1</div>
<div>2</div>
</>
);
}
<></> === <React.Fragment></React.Fragment> ๋ฆฌ์กํธ์์ ์ ๊ณตํ๋ Fragment
์ฌ๋ฌ์ค์ ์ฝ๋๋ฅผ return ํ ๊ฒฝ์ฐ ()
์๊ดํธ ์ฌ์ฉ
()
์๊ดํธ ์ฌ์ฉexport default function App() {
return (
// ๐๐ป
<>
<div>๋ฆฌ์กํธ</div>
<div>๋ฆฌ์กํธ2</div>
</>
);
}
๋ฐ๋์ ํ๊ทธ๋ ๋ซ์์ค๋ค
import Button from './components/Button';
export default function App() {
return (
<>
<Button />
<Button></Button>
</>
);
}
ํํ์์ {}
์ฌ์ฉํ๋ค
{}
์ฌ์ฉํ๋ค๊ฐ์ฒด,๋ฐฐ์ด,Boolean ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์ ๋ถ๊ฐ๋ฅ
export default function Button() {
const text = '๋ฒํผ์
๋๋ค!';
return <div>{text}</div>;
}
Props์ ํ์ฉ๋ฒ
1. ๋งค๊ฐ๋ณ์ ๊ตฌ์กฐ๋ถํดํ ๋น
โญ๏ธ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๋ฐฉ์
export default function PropsButton({
type,
name,
disabled,
children,
}: TButtonProps) {
return (
<button type={type} name={name} disabled={disabled}>
{children}
</button>
);
}
2. ์ปดํฌ๋ํธ ๋ด์์ props์ ๊ตฌ์กฐ๋ถํดํ ๋น
export default function PropsButton(props: TButtonProps) {
const { type, disabled, children } = props;
return (
<button type={type} disabled={disabled}>
{children}
</button>
);
}
3.๋๋จธ์ง ๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํ ๊ตฌ์กฐ๋ถํดํ ๋น
type TButtonProps = React.ComponentProps<'button'> & {
children: ReactNode,
};
export default function PropsButton(props: TButtonProps) {
const { children, ...restButtonProps } = props;
return <button {...restButtonProps}>{children}</button>;
}
Last updated