Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right React Component PropTypes for es6 (Weak) Maps, Sets

Tags:

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).

like image 327
Vitali Zaidman Avatar asked Mar 29 '16 09:03

Vitali Zaidman


People also ask

What are the PropTypes in React?

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.

Can we use PropTypes in functional component?

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.

Which PropTypes in React form component will validate a value for an attribute is passed and of type function?

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.


1 Answers

elementsMap: p.instanceOf(Map).isRequired 
like image 71
danday74 Avatar answered Oct 13 '22 14:10

danday74