Writing custom property validators
In this final section, you'll see how to build your own custom property validation functions and apply them in the property specification. Generally speaking, you should only implement your own property validator if you absolutely have to. The default validators available in PropTypes
cover a wide range of scenarios.
However, sometimes, you need to make sure that very specific property values are passed to the component. Remember, these will not be run in production mode, so it's perfectly acceptable to iterate over collections. Let's implement a couple of custom validator functions now:
import React from 'react'; const MyComponent = ({ myArray, myNumber, }) => ( <section> <ul> {myArray.map(i => ( <li key={i}>{i}</li> ))} </ul> <p>{myNumber}</p> </section> ); MyComponent...