As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flowtype.org/) or [TypeScript](https://www.typescriptlang.org/) to typecheck your whole application. But even if you don't use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special `propTypes` property:
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flowtype.org/) or [TypeScript](https://www.typescriptlang.org/) to typecheck your whole application. But even if you don't use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special `propTypes` property:
```javascript
```javascript
import PropTypes from 'prop-types';
class Greeting extends React.Component {
class Greeting extends React.Component {
render() {
render() {
return (
return (
@ -21,68 +23,70 @@ class Greeting extends React.Component {
}
}
Greeting.propTypes = {
Greeting.propTypes = {
name: React.PropTypes.string
name: PropTypes.string
};
};
```
```
`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 `React.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.
`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.
### React.PropTypes
### PropTypes
Here is an example documenting the different validators provided:
Here is an example documenting the different validators provided:
```javascript
```javascript
import PropTypes from 'prop-types';
MyComponent.propTypes = {
MyComponent.propTypes = {
// You can declare that a prop is a specific JS primitive. By default, these
// You can declare that a prop is a specific JS primitive. By default, these
// are all optional.
// are all optional.
optionalArray: React.PropTypes.array,
optionalArray: PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalBool: PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalFunc: PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalNumber: PropTypes.number,
optionalObject: React.PropTypes.object,
optionalObject: PropTypes.object,
optionalString: React.PropTypes.string,
optionalString: PropTypes.string,
optionalSymbol: React.PropTypes.symbol,
optionalSymbol: PropTypes.symbol,
// Anything that can be rendered: numbers, strings, elements or an array
// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
// (or fragment) containing these types.
optionalNode: React.PropTypes.node,
optionalNode: PropTypes.node,
// A React element.
// A React element.
optionalElement: React.PropTypes.element,
optionalElement: PropTypes.element,
// You can also declare that a prop is an instance of a class. This uses
// You can also declare that a prop is an instance of a class. This uses