Add “Don't Call PropTypes” warning (#7219)
Dan Abramov committed
Jul 29, 2016 at 20:04 UTC
8329856a3f8a357e7c217bd02cade89cb84fa7b0
1 file changed
+93
docs/warnings/dont-call-proptypes.md
new
+93
@@ -0,0 +1,93 @@
1
+---
2
+title: Don't Call PropTypes Warning
3
+layout: single
4
+---
5
+
6
+In a future major release of React, the code that implements PropType validation functions will be stripped in production. Once this happens, any code that calls these functions manually (that isn't stripped in production) will throw an error.
7
+
8
+### Declaring PropTypes is still fine
9
+
10
+The normal usage of PropTypes is still supported:
11
+
12
+```javascript
13
+Button.propTypes = {
14
+ highlighted: React.PropTypes.bool
15
+};
16
+```
17
+
18
+Nothing changes here.
19
+
20
+### Don’t call PropTypes directly
21
+
22
+Using PropTypes in any other way than annotating React components with them is no longer supported:
23
+
24
+```javascript
25
+var apiShape = React.PropTypes.shape({
26
+ body: React.PropTypes.object,
27
+ statusCode: React.PropTypes.number.isRequired
28
+}).isRequired;
29
+
30
+// Not supported!
31
+var error = apiShape(json, 'response');
32
+```
33
+
34
+If you depend on using PropTypes like this, we encourage you to use or create a fork of PropTypes (such as [these](https://github.com/aackerman/PropTypes) [two](https://github.com/developit/proptypes) packages).
35
+
36
+If you don't fix the warning, this code will crash in production with React 16.
37
+
38
+### If you don't call PropTypes directly but still get the warning
39
+
40
+Inspect the stack trace produced by the warning. You will find the component definition responsible for the PropTypes direct call. Most likely, the issue is due to third-party PropTypes that wrap React’s PropTypes, for example:
41
+
42
+```js
43
+Button.propTypes = {
44
+ highlighted: ThirdPartyPropTypes.deprecated(
45
+ React.PropTypes.bool,
46
+ 'Use `active` prop instead'
47
+ )
48
+}
49
+```
50
+
51
+In this case, `ThirdPartyPropTypes.deprecated` is a wrapper calling `React.PropTypes.bool`. This pattern by itself is fine, but triggers a false positive because React thinks you are calling PropTypes directly. The next section explains how to fix this problem for a library implementing something like `ThirdPartyPropTypes`. If it's not a library you wrote, you can file an issue against it.
52
+
53
+### Fixing the false positive in third party PropTypes
54
+
55
+If you are an author of a third party PropTypes library and you let consumers wrap existing React PropTypes, they might start seeing this warning coming from your library. This happens because React doesn't see a "secret" last argument that [it passes](https://github.com/facebook/react/pull/7132) to detect manual PropTypes calls.
56
+
57
+Here is how to fix it. We will use `deprecated` from [react-bootstrap/react-prop-types](https://github.com/react-bootstrap/react-prop-types/blob/0d1cd3a49a93e513325e3258b28a82ce7d38e690/src/deprecated.js) as an example. The current implementation only passes down the `props`, `propName`, and `componentName` arguments:
58
+
59
+```javascript
60
+export default function deprecated(propType, explanation) {
61
+ return function validate(props, propName, componentName) {
62
+ if (props[propName] != null) {
63
+ const message = `"${propName}" property of "${componentName}" has been deprecated.\n${explanation}`;
64
+ if (!warned[message]) {
65
+ warning(false, message);
66
+ warned[message] = true;
67
+ }
68
+ }
69
+
70
+ return propType(props, propName, componentName);
71
+ };
72
+}
73
+```
74
+
75
+In order to fix the false positive, make sure you pass **all** arguments down to the wrapped PropType. This is easy to do with the ES6 `...rest` notation:
76
+
77
+```javascript
78
+export default function deprecated(propType, explanation) {
79
+ return function validate(props, propName, componentName, ...rest) { // Note ...rest here
80
+ if (props[propName] != null) {
81
+ const message = `"${propName}" property of "${componentName}" has been deprecated.\n${explanation}`;
82
+ if (!warned[message]) {
83
+ warning(false, message);
84
+ warned[message] = true;
85
+ }
86
+ }
87
+
88
+ return propType(props, propName, componentName, ...rest); // and here
89
+ };
90
+}
91
+```
92
+
93
+This will silence the warning.