Skip to content

Commit 8434ddf

Browse files
bvaughnrhagigi
authored andcommitted
StrictMode should not warn about polyfilled getSnapshotBeforeUpdate (facebook#12647)
* Installed 3.x release of react-lifecycles-compat * Updated ReactComponentLifeCycle-test and ReactDOMServerLifecycles-test to cover both polyfilled lifecycles in StrictMode * Updated StrictMode warnings to not warn about polyfilled getSnapshotBeforeUpdate
1 parent 169bc35 commit 8434ddf

File tree

5 files changed

+55
-30
lines changed

5 files changed

+55
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"prettier": "1.11.1",
8383
"prop-types": "^15.6.0",
8484
"random-seed": "^0.3.0",
85-
"react-lifecycles-compat": "^1.0.2",
85+
"react-lifecycles-compat": "^3.0.2",
8686
"rimraf": "^2.6.1",
8787
"rollup": "^0.52.1",
8888
"rollup-plugin-babel": "^3.0.1",

packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.internal.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ describe('ReactComponentLifeCycle', () => {
6363
});
6464

6565
describe('react-lifecycles-compat', () => {
66-
const polyfill = require('react-lifecycles-compat');
66+
const {polyfill} = require('react-lifecycles-compat');
6767

68-
it('should not warn about deprecated cWM/cWRP for polyfilled components', () => {
68+
it('should not warn for components with polyfilled getDerivedStateFromProps', () => {
6969
class PolyfilledComponent extends React.Component {
7070
state = {};
7171
static getDerivedStateFromProps() {
@@ -79,17 +79,20 @@ describe('ReactComponentLifeCycle', () => {
7979
polyfill(PolyfilledComponent);
8080

8181
const container = document.createElement('div');
82-
ReactDOM.render(<PolyfilledComponent />, container);
82+
ReactDOM.render(
83+
<React.StrictMode>
84+
<PolyfilledComponent />
85+
</React.StrictMode>,
86+
container,
87+
);
8388
});
8489

85-
it('should not warn about unsafe lifecycles within "strict" tree for polyfilled components', () => {
86-
const {StrictMode} = React;
87-
90+
it('should not warn for components with polyfilled getSnapshotBeforeUpdate', () => {
8891
class PolyfilledComponent extends React.Component {
89-
state = {};
90-
static getDerivedStateFromProps() {
92+
getSnapshotBeforeUpdate() {
9193
return null;
9294
}
95+
componentDidUpdate() {}
9396
render() {
9497
return null;
9598
}
@@ -99,9 +102,9 @@ describe('ReactComponentLifeCycle', () => {
99102

100103
const container = document.createElement('div');
101104
ReactDOM.render(
102-
<StrictMode>
105+
<React.StrictMode>
103106
<PolyfilledComponent />
104-
</StrictMode>,
107+
</React.StrictMode>,
105108
container,
106109
);
107110
});

packages/react-dom/src/__tests__/ReactDOMServerLifecycles-test.internal.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ describe('ReactDOMServerLifecycles', () => {
6969
});
7070

7171
describe('react-lifecycles-compat', () => {
72-
const polyfill = require('react-lifecycles-compat');
72+
const {polyfill} = require('react-lifecycles-compat');
7373

74-
it('should not warn about deprecated cWM/cWRP for polyfilled components', () => {
74+
it('should not warn for components with polyfilled getDerivedStateFromProps', () => {
7575
class PolyfilledComponent extends React.Component {
7676
state = {};
7777
static getDerivedStateFromProps() {
@@ -84,7 +84,35 @@ describe('ReactDOMServerLifecycles', () => {
8484

8585
polyfill(PolyfilledComponent);
8686

87-
ReactDOMServer.renderToString(<PolyfilledComponent />);
87+
const container = document.createElement('div');
88+
ReactDOMServer.renderToString(
89+
<React.StrictMode>
90+
<PolyfilledComponent />
91+
</React.StrictMode>,
92+
container,
93+
);
94+
});
95+
96+
it('should not warn for components with polyfilled getSnapshotBeforeUpdate', () => {
97+
class PolyfilledComponent extends React.Component {
98+
getSnapshotBeforeUpdate() {
99+
return null;
100+
}
101+
componentDidUpdate() {}
102+
render() {
103+
return null;
104+
}
105+
}
106+
107+
polyfill(PolyfilledComponent);
108+
109+
const container = document.createElement('div');
110+
ReactDOMServer.renderToString(
111+
<React.StrictMode>
112+
<PolyfilledComponent />
113+
</React.StrictMode>,
114+
container,
115+
);
88116
});
89117
});
90118
});

packages/react-reconciler/src/ReactStrictModeWarnings.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,6 @@ if (__DEV__) {
236236
return;
237237
}
238238

239-
// Don't warn about react-lifecycles-compat polyfilled components.
240-
// Note that it is sufficient to check for the presence of a
241-
// single lifecycle, componentWillMount, with the polyfill flag.
242-
if (
243-
typeof instance.componentWillMount === 'function' &&
244-
instance.componentWillMount.__suppressDeprecationWarning === true
245-
) {
246-
return;
247-
}
248-
249239
let warningsForRoot;
250240
if (!pendingUnsafeLifecycleWarnings.has(strictRoot)) {
251241
warningsForRoot = {
@@ -261,19 +251,23 @@ if (__DEV__) {
261251

262252
const unsafeLifecycles = [];
263253
if (
264-
typeof instance.componentWillMount === 'function' ||
254+
(typeof instance.componentWillMount === 'function' &&
255+
instance.componentWillMount.__suppressDeprecationWarning !== true) ||
265256
typeof instance.UNSAFE_componentWillMount === 'function'
266257
) {
267258
unsafeLifecycles.push('UNSAFE_componentWillMount');
268259
}
269260
if (
270-
typeof instance.componentWillReceiveProps === 'function' ||
261+
(typeof instance.componentWillReceiveProps === 'function' &&
262+
instance.componentWillReceiveProps.__suppressDeprecationWarning !==
263+
true) ||
271264
typeof instance.UNSAFE_componentWillReceiveProps === 'function'
272265
) {
273266
unsafeLifecycles.push('UNSAFE_componentWillReceiveProps');
274267
}
275268
if (
276-
typeof instance.componentWillUpdate === 'function' ||
269+
(typeof instance.componentWillUpdate === 'function' &&
270+
instance.componentWillUpdate.__suppressDeprecationWarning !== true) ||
277271
typeof instance.UNSAFE_componentWillUpdate === 'function'
278272
) {
279273
unsafeLifecycles.push('UNSAFE_componentWillUpdate');

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4434,9 +4434,9 @@ [email protected]:
44344434
object-assign "^4.1.0"
44354435
prop-types "~15.5.7"
44364436

4437-
react-lifecycles-compat@^1.0.2:
4438-
version "1.0.2"
4439-
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-1.0.2.tgz#551d8b1d156346e5fcf30ffac9b32ce3f78b8850"
4437+
react-lifecycles-compat@^3.0.2:
4438+
version "3.0.2"
4439+
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.2.tgz#7279047275bd727a912e25f734c0559527e84eff"
44404440

44414441
44424442
version "15.5.4"

0 commit comments

Comments
 (0)