1MyComponent.propTypes = {
2 items: PropTypes.arrayOf(
3 PropTypes.shape({
4 code: PropTypes.string,
5 id: PropTypes.number,
6 })
7 ),
8};
1import React from 'react';
2import { PropTypes } from 'prop-types';
3
4const student = (props) => {
5 return (
6 <div>
7 <p>Student Name: {props.name}</p>
8 <p>Age: {props.age}</p>
9 </div>
10 );
11};
12
13student.propTypes = {
14 name: PropTypes.string,
15 age: PropTypes.number
16};
17
18export default student;
1import PropTypes from 'prop-types';
2
3class Greeting extends React.Component {
4 render() {
5 return (
6 <h1>Hello, {this.props.name}</h1>
7 );
8 }
9}
10
11Greeting.propTypes = {
12 name: PropTypes.string
13};
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