MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Prop Types


propTypes allows you to specify what props your component needs and the type they should be. Your component will work without setting propTypes, but it is good practice to define these as it will make your component more readable, act as documentation to other developers who are reading your component, and during development, React will warn you if you you try to set a prop which is a different type to the definition you have set for it.

Some primitive propTypes and commonly useable propTypes are -

 optionalArray: React.PropTypes.array,
 optionalBool: React.PropTypes.bool,
 optionalFunc: React.PropTypes.func,
 optionalNumber: React.PropTypes.number,
 optionalObject: React.PropTypes.object,
 optionalString: React.PropTypes.string,
 optionalSymbol: React.PropTypes.symbol

If you attach isRequired to any propType then that prop must be supplied while creating the instance of that component. If you don't provide the required propTypes then component instance can not be created.

Syntax


ES5


var MyClass = React.createClass({
 propTypes: {
 randomObject: React.PropTypes.object,
callback: React.PropTypes.func.isRequired,
 ...
 }
}

ES6


class MyClass extends React.Component {...}
MyClass.propTypes = {
 randomObject: React.PropTypes.object,
 callback: React.PropTypes.func.isRequired,
 ...
};

ES7


class MyClass extends React.Component {
 static propTypes = {
 randomObject: React.PropTypes.object,
 callback: React.PropTypes.func.isRequired,
 ...
 };
}

More complex props validation


In the same way, PropTypes allows you to specify more complex validation

Validating an object


...
 randomObject: React.PropTypes.shape({
 id: React.PropTypes.number.isRequired,
 text: React.PropTypes.string,
 }).isRequired,
...

Validating on array of objects


...
 arrayOfObjects: React.PropTypes.arrayOf(React.PropTypes.shape({
 id: React.PropTypes.number.isRequired,
 text: React.PropTypes.string,
 })).isRequired,
...


Conclusion

In this page (written and validated by ) you learned about React Prop Types . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Passing down props using spread operator.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.