React - prop-types (Type checking)

About

Javascript - (Static) Typing (Checker) in React

PropTypes exports a range of validators that can be used to make sure the data you receive is valid. In this example, we're using PropTypes.string. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console. For performance reasons, propTypes is only checked in development mode.

Example

The name property must be a string

import PropTypes from 'prop-types';

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.propTypes = {
  name: PropTypes.string
};

List

  • PropTypes.string
  • PropTypes.bool,
  • PropTypes.func,
  • elementType,
  • PropTypes.oneOf(['button', 'reset', 'submit']),
  • PropTypes.func.isRequired,
  • PropTypes.object.isRequired
  • PropTypes.arrayOf
PropTypes.arrayOf(
        PropTypes.shape({
            id: PropTypes.string.isRequired,
            title: PropTypes.string.isRequired,
        })
    ).isRequired,

Documentation / Reference





Discover More
Javascript Object - Schema

in Javascript Object schema validation - check schema validity - - Yup is a JavaScript schema builder for value parsing and validation. Define a schema, transform...
React Props Chrome Plugin
React - Props

props is the only object argument passed from a component to another component. Props are similar to State, but is public and not fully controlled by the component. You can also pass arguments to...



Share this page:
Follow us:
Task Runner