๐ค tsx ํ์ผ์๋ Server ๋ client ๋ FC ๋ฅผ ๋ถ์ฌ์ผํด ?
๐ง ์ฅ๋จ์
TSX ํ์ผ์์ ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ ๋ FC (Functional Component) ํ์ ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ ํ ์ฌํญ์ ๋๋ค. ์ด๋ ๊ฐ๋ฐ์์ ์ ํธ๋์ ํ์ ์ฝ๋ ์คํ์ผ ๊ฐ์ด๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค. FC ํ์ ์ ์ฌ์ฉํ๋ฉด ์ปดํฌ๋ํธ์ Props ํ์ ์ ๋ช ์ํ๊ณ , ๊ธฐ๋ณธ์ ์ผ๋ก children prop์ ํฌํจํ๋ ๋ฑ์ ์ด์ ์ด ์์ต๋๋ค.
์ฅ์
FC ํ์
์ children์ ๊ธฐ๋ณธ์ ์ผ๋ก ํฌํจํ๊ธฐ ๋๋ฌธ์ ์ถ๊ฐ์ ์ธ ํ์
์ ์๊ฐ ํ์ ์์ต๋๋ค.
์ปดํฌ๋ํธ์ Props ํ์
์ ๋ช
ํํ๊ฒ ์ ์ํ ์ ์์ต๋๋ค.
๋จ์
FC ํ์
์ ๊ธฐ๋ณธ์ ์ผ๋ก children์ ํฌํจํ๊ธฐ ๋๋ฌธ์, children์ ์ฌ์ฉํ์ง ์๋ ์ปดํฌ๋ํธ์์๋ children prop์ด ํ์ํ ๊ฒ์ผ๋ก ๋ณด์ผ ์ ์์ต๋๋ค.
์ผ๋ถ ๊ฒฝ์ฐ์๋ ๋ถํ์ํ๊ฒ ๋ณต์กํด ๋ณด์ผ ์ ์์ต๋๋ค.
๊ฒฐ๋ก
๋ชจ๋ ์ปดํฌ๋ํธ์ FC ํ์
์ ์ฌ์ฉํด์ผ ํ๋ ๊ฒ์ ์๋๋ฉฐ, ํ์์ ๋ฐ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํ์ ์ฝ๋ ์คํ์ผ ๊ฐ์ด๋๋ ๊ฐ์ธ์ ์ ํธ๋์ ๋ฐ๋ผ ๊ฒฐ์ ํ๋ฉด ๋ฉ๋๋ค. ๋ง์ฝ FC ํ์
์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ฝ๋์ ์ผ๊ด์ฑ์ ์ ์งํ๊ณ ํ์
์ ์๋ฅผ ๋ ๋ช
ํํ๊ฒ ํ๋ค๋ฉด ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๊ทธ๋ ์ง ์๋ค๋ฉด ๊ตณ์ด ์ฌ์ฉํ ํ์๋ ์์ต๋๋ค.
๐ FC ํ์ ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
FC ํ์ ์ ์ฌ์ฉํ๋ฉด ํ์ ์คํฌ๋ฆฝํธ์ ์ด์ ์ ๋ ์ ํ์ฉํ ์ ์์ต๋๋ค. ํนํ, Props์ ํ์ ์ ๋ช ์์ ์ผ๋ก ์ง์ ํ๊ณ children์ ์๋์ผ๋ก ํฌํจ์ํฌ ์ ์์ต๋๋ค.
import React, { FC } from 'react';
interface MyComponentProps {
title: string;
}
const MyComponent: FC<MyComponentProps> = ({ title, children }) => {
return (
<div>
<h1>{title}</h1>
{children}
</div>
);
};
export default MyComponent;
import React from 'react';
interface MyComponentProps {
title: string;
children?: React.ReactNode;
}
const MyComponent = ({ title, children }: MyComponentProps) => {
return (
<div>
<h1>{title}</h1>
{children}
</div>
);
};
export default MyComponent;