Validating React component properties
In React, there is a way to validate the component properties using the component's propTypes
object:
propTypes: { propertyName: validator }
In this object, you need to specify a property name and a validator function that will determine whether a property is valid or not. React provides some predefined validators for you to reuse. They are all available in the React.PropTypes
object:
React.PropTypes.number
: This will validate whether a property is a number or notReact.PropTypes.string
: This will validate whether a property is a string or notReact.PropTypes.bool
: This will validate whether a property is a Boolean or notReact.PropTypes.object
: This will validate whether a property is an object or notReact.PropTypes.element
: This will validate whether a property is a React element or not
For a complete list of the React.PropTypes
validators, you can check the docs at https://facebook.github.io/react/docs/reusable-components.html#prop-validation.
By default...