
๐ ๋ฌด์จ ํ์ ์ ์จ์ผํ์ง..? ๐ค
TypeScript ์ ํจ๊ป React ํ๋ก์ ํธ๋ฅผ ๊ฐ๋ฐํ๋ค๋ณด๋ฉด ์ปดํฌ๋ํธ์์
children ์ prop ์ผ๋ก ๋ฐ์์ ์ฌ์ฉํ๊ณ ์ด์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ํด์ค์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
export default function Component({ children }) {
return (
<div>
์์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํฉ๋๋ค.
{ children }
</div>
)
}
์ด๋ด๋๋ง๋ค ํญ์ ๊ณ ๋ฏผ์ธ ๋ถ๋ถ์ด children ์ ์ด๋ค ํ์
์ ๋ช
์ํด์ผ ํ๋ ๊ฒ์ธ๊ฐ? ์ ๋ํ ๊ฒ์
๋๋ค.
์ด๋ฒ ํฌ์คํธ์์๋ ์ด ๋ถ๋ถ์ ๋ํ ๊ณ ๋ฏผ์ ํด๊ฒฐํ๊ณ ์!
React ์ปดํฌ๋ํธ์ children ์ ํ ๋น ๊ฐ๋ฅํ ํ์
๋ค์ ๋ํด์ ๊ฐ๋จํ๊ฒ ์ ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค.
๐ React ์์ ์ ๊ณตํ๋ ์ฌ๋ฌ๊ฐ์ง children ํ์ ๋ค
1๏ธโฃ JSX.Element
declare global {
namespace JSX {
interface Element extends React.ReactElement<any, any> { }
// ...
}
}
JSX.Element ๋ ReactElement ๋ฅผ ์์๋ฐ๋ ์ธํฐํ์ด์ค์
๋๋ค.
์ด๋ ReactElement ์ type ๊ณผ props ๊ฐ any ํ์
์ผ๋ก ์ ์๋์ด์์ต๋๋ค.
๋ค์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ JSX ๋ฅผ ๊ทธ๋ค๋ง์ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์๊ธฐ ๋๋ฌธ์
React ๋ ์ ์ญ ๋ค์์คํ์ด์ค์ ์์ ๊ฐ์ด JSX ํ์
์ ์ ์ํ ๊ฒ์
๋๋ค.
2๏ธโฃ React.ReactElement
interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
type: T;
props: P;
key: Key | null;
}
React.createElement ํจ์๋ฅผ ํธ์ถํ๋ฉด ReactElement ์์๊ฐ ๋ฐํ๋ฉ๋๋ค.
๋ํ React ์ ํจ์ํ ์ปดํฌ๋ํธ์ ๋ฐํ ํ์
์ด๊ธฐ๋ ํฉ๋๋ค.
ํด๋น ํ์ ์ ์ค์ง JSX ์์๋ง์ ๋ฐ๋๋ก ๊ฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ์
๋ง์ฝ ๋ค์๊ณผ ๊ฐ์ด ์์๊ฐ์ ๋ถ์ฌํ๋ฉด ํ์ ์๋ฌ๋ฅผ ๋ฑ์ด๋ด๊ฒ ๋ฉ๋๋ค.

์ด ๋๋ฌธ์ ๋ง์ฝ ์์๊ฐ์ ํ์ฉํ์ง ์๊ณ JSX ์ปดํ์ผ๋ React ์ปดํฌ๋ํธ๋ง ํ์ฉํ๊ณ ์ถ์ ๊ฒฝ์ฐ
ReactElement ๋ฅผ ํตํด children ํ์
์ ์ง์ ํ๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค ๐
3๏ธโฃ React.ReactChild (โ ๏ธ deprecated)
type ReactChild = ReactElement | string | number;
React 18 ๋ถํฐ ์ฌ์ฉ์ ์ง์ํ๋ ํ์
์
๋๋ค.
ReactElement ๋ฅผ ํฌํจํ์ฌ string ๊ณผ number ํ์
์ ํฌํจํ๊ณ ์์ต๋๋ค.
React ์ ์ง์ ์ ์ผ๋ก ์ฐ๊ด์ด ์๋ ํด๋น ํ์
์ ์ธ๋ผ์ธํํ์ฌ
๋ณด๋ค ๋ช ํํ ํ์ดํ์ ํ๋๋ก ํ๊ธฐ ์ํ ์๋๋ก ์๊ฐ๋ฉ๋๋ค.
4๏ธโฃ React.ReactNode
type ReactNode = ReactElement | string | number | ReactFragment | ReactPortal | boolean | null | undefined;
๊ฐ์ฅ ๊ด๋ฒ์ํ๊ฒ ์ฌ์ฉํ ์ ์๋ React ์ปดํฌ๋ํธํ์ ์ ๋๋ค.
๋ํ ํด๋์คํ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ ๋ฐํ ํ์ ์ด๊ธฐ๋ ํฉ๋๋ค.
๋ค์์ @types/react ์ ์ ์๋์ด์๋ ReactNode ํ์
์ ๋ํ ๋ช
์ธ์
๋๋ค.
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L431
class Component<P, S> {
// ...
setState<K extends keyof S>(
state: ((prevState: Readonly<S>, props: Readonly<P>) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null),
callback?: () => void
): void;
forceUpdate(callback?: () => void): void;
render(): ReactNode; // <-- โ
ReactNode!
// ...
}
string, number, null, undefined ๋ฑ์ ์์ํ์
๋ค๊ณผ
ReactElement, ReactFragment ๋ฑ์ ํ์
์ ๋ชจ๋ ํฌํจํ๊ณ ์์ต๋๋ค.
ReactNodeArray ๋ deprecated ๋์๊ธฐ ๋๋ฌธ์ ๋ง์ฝ ๋ฐฐ์ด ํํ๊ฐ ํ์ํ๋ค๋ฉด
ReactNode[] ํํ๋ก ํ์
์ ์ ์ํฉ๋๋ค.
5๏ธโฃ React.PropsWithChildren
type PropsWithChildren<P = unknown> = P & { children?: ReactNode | undefined };
React 16 ๋ถํฐ ์ง์๋๊ธฐ ์์ํ ํ์
์
๋๋ค.
ํด๋น ํ์
์ ํตํด children ์ ์ ํ์ ์ผ๋ก ํฌํจํ๋ ์ปดํฌ๋ํธ์ ํ์ดํ์ ๊ฐ์ถ๋ฆด ์ ์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ children ์ด ์ต์
๋ํ๋ค๋ณด๋ ๋ง์ฝ ์ปดํฌ๋ํธ์ children ์ด ํ์๊ฐ์ธ ๊ฒฝ์ฐ์๋
์ ํฉํ์ง ์์ ํ์ ์ผ ์๋ ์๊ฒ ๋ค๊ณ ์๊ฐ์ด ๋๋ค์ ๐ค
6๏ธโฃ React.FC (๐จ consider not to use!)
type FC<P = {}> = FunctionComponent<P>;
interface FunctionComponent<P = {}> {
(props: P, context?: any): ReactElement<any, any> | null;
propTypes?: WeakValidationMap<P> | undefined;
contextTypes?: ValidationMap<any> | undefined;
defaultProps?: Partial<P> | undefined;
displayName?: string | undefined;
}
์ด์ ์๋ ํจ์ํ ์ปดํฌ๋ํธ๋ฅผ ํ์ดํํ ๋ FC ์ ๋ค๋ฆญ ํ์
์ ํตํด ํ๊ณ ๋ ํ์ต๋๋ค.
React 17 ์์๋ props ๊ฐ PropsWithChildren ๋ผ์ children ๊ฐ ReactNode ํ์
์ ๊ฐ์ก์ง๋ง
React 18 ๋ถํฐ๋ props ์ ๋ํ ํ์
์ด ์ ๋ค๋ฆญ์ผ๋ก ๋ฐ๋๋ฉด์ ์ง์ ์ง์ ํด์ค์ผํฉ๋๋ค.
๋ค๋ง React.FC ์ ๊ฒฝ์ฐ ์๋ฌต์ ์ผ๋ก children ์ ํฌํจํ๋ ๋ฑ
์ฅ์ ๋ณด๋ค ๋ง์ ์ฌ๋ฌ๊ฐ์ง ๋จ์ ๋ค ๋๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ง์ํ๊ณ ์์ด
๋ค๋ฅธ ํ์ ๋ค์ ํ์ฉํด์ ์ปดํฌ๋ํธ์ ๋ํ ํ์ ์ ์ ์ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๐ ์ฐธ๊ณ ์๋ฃ
Using React children prop with TypeScript
ReactNode, ReactChild, ReactElement ํ์ ๋น๊ต
When to use JSX.Element vs ReactNode vs ReactElement?
DefinitelyTyped/types/react at master · DefinitelyTyped/DefinitelyTyped
'๐จโ๐ป web.dev > frontend' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| async function ์๋ณํ๊ธฐ (1) | 2022.08.08 |
|---|---|
| Next.js API Routes ์์๋ณด๊ธฐ (2) | 2022.07.26 |
| [JavaScript] Debounce ์ Throttle ์ ๋ํด ์์๋ณด์ (0) | 2022.07.09 |
| [TypeScript] enum ๊ณผ union type, ์ธ์ ์จ์ผํ ๊น? (2) | 2022.07.09 |
| React ์ค๋๋ ํด๋ก์ (Stale Closure) ์ด์ ํด๊ฒฐํ๊ธฐ (0) | 2022.05.11 |
๐ฌ ๋๊ธ