Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 709 Bytes

run-type-checking.md

File metadata and controls

40 lines (30 loc) · 709 Bytes

How to run type checking on the props

To run typechecking on the props for a component, you can assign the special propTypes property:

import PropTypes from 'prop-types';

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

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

This also works with a function component:

import PropTypes from 'prop-types';

const Greeting = (props) => {
  return (
    <h1>Hello, {props.name}</h1>
  );
}

Greeting.propTypes = {
  type: PropTypes.string.isRequired
}

export default Greeting;

References: