What Are PropTypes In React?

Question
  • Prior to React 15.5.0, a utility named PropTypes was available as part of the React package, which provided a lot of validators for configuring type definitions for component props. It could be accessed with React.PropTypes.
  • However, in later versions of React, this utility has been moved to a separate package named prop-types, so you need to add it as a dependency for your project in order to get access to the PropTypes utility.
  • Props and PropTypes are important mechanisms for passing information between React components. it’s always a good practice to validate the data we are getting through props by using PropTypes.
  • PropTypes are a mechanism to ensure that components use the correct data type and pass the right data, and that components use the right type of props, and that receiving components receive the right type of props.

Available validators:

  • 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
3 years 2021-08-07T05:51:58+00:00 16 views 0

Leave an answer

Browse
Browse