FRONTEND/REACTJS

react 18 typescript type에 PropsWithChildren가 추가되었습니다

나를찾는아이 2022. 10. 31. 18:58
728x90
반응형

react 18 types에 PropsWithChildren 이라는 타입이 추가되었습니다

 

말그대로 children을 포함한 props를 가리키는 타입입니다

 

코드를 보면 이런식으로 선언이 되어있습니다

 

type PropsWithChildren<P = unknown> = P & { children?: ReactNode | undefined };

 

그래서 react 컴포넌트에서 사용할때 이런식으로 사용할수 있습니다

 

import React, { PropsWithChildren } from "react";

type ComponentProps = {
  someProperty: string;
};

function Component({ someProperty, children }: PropsWithChildren<ComponentProps>) {
  // ...
}

 

좀더 코드보기가 깔끔해지네요

728x90
반응형