Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit ceb4a9e

Browse files
authored
Merge pull request #20 from reactjs/revert-breaking-change
Revert accidental breaking change for 15.5
2 parents 8b3d352 + fff750d commit ceb4a9e

12 files changed

+2961
-525
lines changed

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,42 @@ If you prefer a `<script>` tag, you can get it from `window.PropTypes` global:
2525
<script src="https://unpkg.com/prop-types/prop-types.min.js"></script>
2626
```
2727

28-
## Difference from `React.PropTypes`: Development and Production Versions
28+
## Difference from `React.PropTypes`: Don’t Call Validator Functions
2929

30-
In production, **all validator functions are replaced with empty functions that throw an error**. This is done to optimize the bundle size. This is new behavior, and you will only encounter it when you migrate from `React.PropTypes` to the `prop-types` package. For the vast majority of components, this doesn’t matter, and if you didn’t see [this warning](https://facebook.github.io/react/warnings/dont-call-proptypes.html) in your components, your code is safe to migrate. This is not a breaking change in React because you are only opting into this change for a component by explicitly changing your imports to use `prop-types`.
30+
When you migrate components to use the standalone `prop-types`, **all validator functions will start throwing an error if you call them directly**. This makes sure that nobody relies on them in production code, and it is safe to strip their implementations to optimize the bundle size.
3131

32-
Don’t call the validator functions manually in your code. React automatically calls `PropTypes` validators declared on your components in development version, and it won’t call them in production. **If you absolutely need to trigger the validation manually**, call `PropTypes.checkPropTypes(propTypes, props, location, componentName)`. Unlike the validators themselves, this function is safe to call in production, as it will be replaced by an empty function.
32+
Code like this is still fine:
33+
34+
```js
35+
MyComponent.propTypes = {
36+
myProp: PropTypes.bool
37+
};
38+
```
39+
40+
However, code like this will not work with the `prop-types` package:
41+
42+
```js
43+
// Will not work with `prop-types` package!
44+
var errorOrNull = PropTypes.bool(42, 'myProp', 'MyComponent', 'prop');
45+
```
46+
47+
It will throw an error:
48+
49+
```
50+
Calling PropTypes validators directly is not supported by the `prop-types` package.
51+
Use PropTypes.checkPropTypes() to call them.
52+
```
53+
54+
This is new behavior, and you will only encounter it when you migrate from `React.PropTypes` to the `prop-types` package. For the vast majority of components, this doesn’t matter, and if you didn’t see [this warning](https://facebook.github.io/react/warnings/dont-call-proptypes.html) in your components, your code is safe to migrate. This is not a breaking change in React because you are only opting into this change for a component by explicitly changing your imports to use `prop-types`. If you temporarily need the old behavior, you can keep using `React.PropTypes` until React 16.
55+
56+
**If you absolutely need to trigger the validation manually**, call `PropTypes.checkPropTypes()`. Unlike the validators themselves, this function is safe to call in production, as it will be replaced by an empty function:
57+
58+
```js
59+
// Works with standalone PropTypes
60+
PropTypes.checkPropTypes(MyComponent.propTypes, props, 'prop', 'MyComponent');
61+
```
62+
63+
**You might also see this error** if you’re calling a `PropTypes` validator from your own custom `PropTypes` validator. In this case, the fix is to make sure that you are passing *all* of the arguments to the inner function. There is a more in-depth explanation of how to fix it [on this page](https://facebook.github.io/react/warnings/dont-call-proptypes.html#fixing-the-false-positive-in-third-party-proptypes). Alternatively, you can temporarily keep using `React.PropTypes` until React 16, as it would still only warn in this case.
3364

3465
If you use a bundler like Browserify or Webpack, don’t forget to [follow these instructions](https://facebook.github.io/react/docs/installation.html#development-and-production-versions) to correctly bundle your application in development or production mode. Otherwise you’ll ship unnecessary code to your users.
3566

__tests__/PropTypes-test.js renamed to __tests__/PropTypesDevelopmentReact15.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
var React;
1515
var PropTypes;
16-
var checkPropTypes;
1716

1817
function resetWarningCache() {
1918
jest.resetModules();
20-
checkPropTypes = require('../checkPropTypes');
19+
20+
React = require('react');
21+
// This is how React 15 imports `prop-types`.
22+
PropTypes = require('../factory')(React.isValidElement);
2123
}
2224

2325
function getPropTypeWarningMessage(propTypes, object, componentName) {
@@ -27,7 +29,8 @@ function getPropTypeWarningMessage(propTypes, object, componentName) {
2729
console.error.calls.reset();
2830
}
2931
resetWarningCache();
30-
checkPropTypes(propTypes, object, 'prop', 'testComponent');
32+
33+
PropTypes.checkPropTypes(propTypes, object, 'prop', 'testComponent');
3134
const callCount = console.error.calls.count();
3235
if (callCount > 1) {
3336
throw new Error('Too many warnings.');
@@ -103,11 +106,8 @@ function expectWarningInDevelopment(declaration, value) {
103106
console.error.calls.reset();
104107
}
105108

106-
describe('ReactPropTypes', () => {
109+
describe('PropTypesDevelopmentReact15', () => {
107110
beforeEach(() => {
108-
React = require('react');
109-
PropTypes = require('../index');
110-
checkPropTypes = PropTypes.checkPropTypes;
111111
resetWarningCache();
112112
});
113113

@@ -120,7 +120,7 @@ describe('ReactPropTypes', () => {
120120
},
121121
};
122122
const props = {foo: 'foo'};
123-
const returnValue = checkPropTypes(
123+
const returnValue = PropTypes.checkPropTypes(
124124
propTypes,
125125
props,
126126
'prop',
@@ -139,7 +139,7 @@ describe('ReactPropTypes', () => {
139139
},
140140
};
141141
const props = {foo: 'foo'};
142-
const returnValue = checkPropTypes(
142+
const returnValue = PropTypes.checkPropTypes(
143143
propTypes,
144144
props,
145145
'prop',
@@ -839,9 +839,9 @@ describe('ReactPropTypes', () => {
839839

840840
it('should warn if called manually in development', () => {
841841
spyOn(console, 'error');
842-
expect(PropTypes.oneOf(['red', 'blue']), true);
843-
expect(PropTypes.oneOf(['red', 'blue']), null);
844-
expect(PropTypes.oneOf(['red', 'blue']), undefined);
842+
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), true);
843+
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), null);
844+
expectWarningInDevelopment(PropTypes.oneOf(['red', 'blue']), undefined);
845845
});
846846
});
847847

0 commit comments

Comments
 (0)