You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/Component.md
+28Lines changed: 28 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -978,6 +978,34 @@ Defining `defaultProps` in class components is similar to using [default values]
978
978
979
979
---
980
980
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
+
Werecommendusing [TypeScript](https://www.typescriptlang.org/) instead of checking prop types at runtime.
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