1// ES6 class
2class CatComponent extends React.Component {
3 constructor(props) {}
4 render() {
5 return <div>{this.props.catName} Cat,
6 Eye Color: {this.props.eyeColor },
7 Age: {this.props.age}</div>
8 }
9}
10CatComponent.defaultProps = {
11 catName: "Sandy",
12 eyeColor: "deepblue",
13 age: "120"
14}
1import PropTypes from 'prop-types';
2
3MyComponent.propTypes = {
4 // You can declare that a prop is a specific JS type. By default, these
5 // are all optional.
6 optionalArray: PropTypes.array,
7 optionalBool: PropTypes.bool,
8 optionalFunc: PropTypes.func,
9 optionalNumber: PropTypes.number,
10 optionalObject: PropTypes.object,
11 optionalString: PropTypes.string,
12 optionalSymbol: PropTypes.symbol,
13
14 // An object that could be one of many types
15 optionalUnion: PropTypes.oneOfType([
16 PropTypes.string,
17 PropTypes.number,
18 PropTypes.instanceOf(Message)
19 ])
20};
1Basic types:
2- PropTypes.any: The prop can be of any data type
3- PropTypes.bool: The prop should be a Boolean
4- PropTypes.number: The prop should be a number
5- PropTypes.string: The prop should be a string
6- PropTypes.func: The prop should be a function
7- PropTypes.array: The prop should be an array
8- PropTypes.object: The prop should be an object
9- PropTypes.symbol: The prop should be a symbol
10
11Renderable types:
12- PropTypes.node: The prop should be anything that can be rendered by React
13 a number, string, element, or array (or fragment) containing these types
14- PropTypes.element: The prop should be a React element
15
16Instance types:
17- PropTypes.instanceOf(class): The prop should be an instance of class
18
19Multiple types:
20- PropTypes.oneOf: The prop is limited to a specified set of values,
21 treating it like an enum
22- PropTypes.oneOfType: The prop should be one of a specified set of
23 types, behaving like a union of types
24
25Collection types:
26- PropTypes.arrayOf: ensures that the prop is an array in which all
27 items match the specified type.
28- PropTypes.objectOf: ensures that the prop is an object in which all
29 property values match the specified type.
30- PropTypes.shape: ensures that the prop is an object that contains a set
31 of specified keys with values of the specified types.
32- PropTypes.exact: use for strict (or exact) object matching