Skip to content

Commit 1ed251f

Browse files
committed
Document that prop-types accepts static types, too
1 parent 932d947 commit 1ed251f

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

docs/rules/prop-types.md

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Prevent missing props validation in a React component definition (react/prop-types)
22

3-
PropTypes improve the reusability of your component by validating the received data.
3+
Defining types for component props improves reusability of your components by
4+
validating received data. It can warn other developers if they make a mistake while reusing the component with improper data type.
45

5-
It can warn other developers if they make a mistake while reusing the component with improper data type.
6+
You can provide types either in runtime types using [PropTypes] or statically
7+
using [TypeScript] or [Flow]. This rule will validate your prop types regardless
8+
of how you're defining them.
69

710
## Rule Details
811

@@ -36,6 +39,18 @@ Hello.propTypes = {
3639
}
3740
```
3841

42+
In TypeScript:
43+
44+
```tsx
45+
interface Props = {
46+
age: number
47+
}
48+
function Hello({ name }: Props) {
49+
return <div>Hello {name}</div>;
50+
// 'name' type is missing in props validation
51+
}
52+
```
53+
3954
Examples of correct usage without warnings:
4055

4156
```jsx
@@ -76,6 +91,19 @@ class HelloEs6WithPublicClassField extends React.Component {
7691
}
7792
```
7893

94+
In Flow:
95+
96+
```tsx
97+
type Props = {
98+
name: string
99+
}
100+
class Hello extends React.Component<Props> {
101+
render() {
102+
return <div>Hello {this.props.name}</div>;
103+
}
104+
}
105+
```
106+
79107
The following patterns are **not** considered warnings:
80108

81109
```jsx
@@ -115,11 +143,11 @@ As it aptly noticed in
115143

116144
> Why should children be an exception?
117145
> Most components don't need `this.props.children`, so that makes it extra important
118-
to document `children` in the propTypes.
146+
to document `children` in the prop types.
119147

120-
Generally, you should use `PropTypes.node` for `children`. It accepts
121-
anything that can be rendered: numbers, strings, elements or an array containing
122-
these types.
148+
Generally, you should use `PropTypes.node` or static type `React.Node` for
149+
`children`. It accepts anything that can be rendered: numbers, strings, elements
150+
or an array containing these types.
123151

124152
Since 2.0.0 children is no longer ignored for props validation.
125153

@@ -132,3 +160,7 @@ For now we should detect components created with:
132160
* a function that return JSX or the result of a `React.createElement` call.
133161
* `createReactClass()`
134162
* an ES6 class that inherit from `React.Component` or `Component`
163+
164+
[PropTypes]: https://reactjs.org/docs/typechecking-with-proptypes.html
165+
[TypeScript]: http://www.typescriptlang.org/
166+
[Flow]: https://flow.org/

0 commit comments

Comments
 (0)