White papers show how to do this with ES6 component classes, but the same goes for stateless functional components.
First, npm install / yarn add new package of prop types , if you havenβt already.
Then add your propTypes (and defaultProps, if necessary) after the stateless functional component has been defined before exporting it.
import React from "react"; import PropTypes from "prop-types"; const Header = ({ name }) => <div>hi {name}</div>; Header.propTypes = { name: PropTypes.string }; // Same approach for defaultProps too Header.defaultProps = { name: "Alan" }; export default Header;
Michael
source share