Skip to content

Commit 52c777b

Browse files
authored
Document propTypes (#5974)
1 parent 496c578 commit 52c777b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/content/reference/react/Component.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,34 @@ Defining `defaultProps` in class components is similar to using [default values]
978978

979979
---
980980

981+
### `static propTypes` {/*static-proptypes*/}
982+
983+
You can define `static propTypes` together with the [`prop-types`](https://www.npmjs.com/package/prop-types) library to declare the types of the props accepted by your component. These types will be checked during rendering and in development only.
984+
985+
```js
986+
import PropTypes from 'prop-types';
987+
988+
class Greeting extends React.Component {
989+
static propTypes = {
990+
name: PropTypes.string
991+
};
992+
993+
render() {
994+
return (
995+
<h1>Hello, {this.props.name}</h1>
996+
);
997+
}
998+
}
999+
```
1000+
1001+
<Note>
1002+
1003+
We recommend using [TypeScript](https://www.typescriptlang.org/) instead of checking prop types at runtime.
1004+
1005+
</Note>
1006+
1007+
---
1008+
9811009
### `static getDerivedStateFromError(error)` {/*static-getderivedstatefromerror*/}
9821010

9831011
If you define `static getDerivedStateFromError`, React will call it when a child component (including distant children) throws an error during rendering. This lets you display an error message instead of clearing the UI.

0 commit comments

Comments
 (0)