Book Image

React.js Essentials

By : Artemij Fedosejev
Book Image

React.js Essentials

By: Artemij Fedosejev

Overview of this book

Table of Contents (18 chapters)
React.js Essentials
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Validating React component properties


In React, there is a way to validate the component properties using the component's propTypes object:

propTypes: {
  propertyName: validator
}

In this object, you need to specify a property name and a validator function that will determine whether a property is valid or not. React provides some predefined validators for you to reuse. They are all available in the React.PropTypes object:

  • React.PropTypes.number: This will validate whether a property is a number or not

  • React.PropTypes.string: This will validate whether a property is a string or not

  • React.PropTypes.bool: This will validate whether a property is a Boolean or not

  • React.PropTypes.object: This will validate whether a property is an object or not

  • React.PropTypes.element: This will validate whether a property is a React element or not

For a complete list of the React.PropTypes validators, you can check the docs at https://facebook.github.io/react/docs/reusable-components.html#prop-validation.

By default...