React - prop-types (Type checking)

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

3 - Example

3.1 - 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
};

4 - 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,

5 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap