티스토리 뷰

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
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함