React PropTypes (기본/Renderable)

김민석·2021년 5월 21일
0

NodeBird 클론코딩

목록 보기
5/10

참고 하였음.
여기는 공식문서

리액트 컴포넌트에서 Props의 타입들을 지정하여 일치하지 않을 시에 오류를 뱉게 할 수 있다.


기본 타입

PropTypes.any: The prop can be of any data type
PropTypes.bool: The prop should be a Boolean
PropTypes.number: The prop should be a number
PropTypes.string: The prop should be a string
PropTypes.func: The prop should be a function
PropTypes.array: The prop should be an array
PropTypes.object: The prop should be an object
PropTypes.symbol: The prop should be a symbol

용법!

Component.propTypes = {
  anyProp: PropTypes.any,
  booleanProp: PropTypes.bool,
  numberProp: PropTypes.number,
  stringProp: PropTypes.string,
  functionProp: PropTypes.func
}

Renderable 타입

이게 좀 헷갈렸다.

nodeelement가 있다.

  • node : 이것은 React가 render 할 수 있는 모든 것을 의미한다고 한다.

  • element : React element, typeprop을 가지는 React 객체를 의미한다.

이 두 가지 type에 isRequired option을 줄 수 있다.

Component.propTypes = {
  children: PropTypes.element.isRequired
}

이러면 단 하나의 element나 node를 갖는 것을 의미한다.


추가!

elementType : Component
element : <Component/>

0개의 댓글