• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    해결됨

ComponentPropsWithoutRef 와 FC<PropsWithChildren<Props>> 의 차이점이 무엇인가요

23.07.26 23:22 작성 조회수 393

0

저는 평소에 아래와 같이 ComponentPropsWithoutRef을 이용하여 children이나 스타일등을 props로 내려받아 사용하고 있엇는데

export interface Props {
  /** 북마크 여부 */
  isBookmark: boolean;
  /** 클릭했을 때 호출할 함수 */
  onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
}

export const Bookmark = (Props: Props & ComponentPropsWithoutRef<"button">) => {
  return (
    <ButtonStyle {...Props}>
      <BookmarkIcon isBookmark={Props.isBookmark} />
    </ButtonStyle>
  );
};

export default Bookmark;

강의에서는 아래와같이 FC<PropsWithChildren<Props>> 형식으로 받아 오던데 둘이 어떤 차이가 있을까요?

interface Props {
  show: boolean;
  onCloseModal: () => void;
  style: CSSProperties;
  closeButton?: boolean;
}

const Menu: FC<PropsWithChildren<Props>> = ({ closeButton, style, show, children, onCloseModal }) => {
  const stopPropagation = useCallback<MouseEventHandler<HTMLDivElement>>((event) => {
    event.stopPropagation();
  }, []);

  if (!show) {
    return null;
  }
  return (
    <CreateMenu onClick={onCloseModal}>
      <div onClick={stopPropagation} style={style}>
        {closeButton && <CloseModalButton onClick={onCloseModal}>&times;</CloseModalButton>}
        {children}
      </div>
    </CreateMenu>
  );
};

답변 1

답변을 작성해보세요.

1

제 타입은 정확히 Props + children인데

ComponentPropsWithoutRef<태그>는 해당 태그의 html 속성들을 의미합니다. button이면 button 태그의 속성들이겠죠. 좋은 타이핑은 아니라고 생각합니다. 쓰지 않는 속성들도 들어 있어서요.