Sibi
Sibi

Reputation: 48654

Making propTypes mandatory and is there stateTypes

In React documentaton, the following has been mentioned under the heading propValidation:

React.createClass({
  propTypes: {
    // You can declare that a prop is a specific JS primitive. By default, these
    // are all optional.
    optionalArray: React.PropTypes.array,
    optionalBool: React.PropTypes.bool,

.....

It is mentioned in the comment that "these are all optional". How can I make it mandatory for that component ?

Also, similar to propTypes is there stateTypes which corresponds to this.state ?

Upvotes: 3

Views: 3027

Answers (1)

taxicala
taxicala

Reputation: 21759

You can declare required props as follows:

React.createClass({
  propTypes: {
    myPropStringRequired: React.PropTypes.string.isRequired,
    myPropArrayRequired: React.PropTypes.array.isRequired,
    myPropBooleanRequired: React.PropTypes.bool.isRequired,
...

As per the second question about stateTypes, please check this question.

Upvotes: 8

Related Questions