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.
Articles Related
Example
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
};
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,