I want to force the user to pass an es6 Map object to a React component using PropTypes, like this for example:
static propTypes = { elementsMap: React.PropTypes.map(React.PropTypes.string, editorPropTypes.element).isRequired, }
But looks like there is nothing like this in React. (The official documentation).
PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.
In this example, we are using a class component, but the same functionality could also be applied to function components, or components created by React.memo or React.forwardRef . PropTypes exports a range of validators that can be used to make sure the data you receive is valid.
App. propTypes is used for props validation in react component. When some of the props are passed with an invalid type, you will get the warnings on JavaScript console.
elementsMap: p.instanceOf(Map).isRequired
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With