Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrictMode should not warn about polyfilled getSnapshotBeforeUpdate #12647

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"prettier": "1.11.1",
"prop-types": "^15.6.0",
"random-seed": "^0.3.0",
"react-lifecycles-compat": "^1.0.2",
"react-lifecycles-compat": "^3.0.2",
"rimraf": "^2.6.1",
"rollup": "^0.52.1",
"rollup-plugin-babel": "^3.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('ReactComponentLifeCycle', () => {
});

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

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

const container = document.createElement('div');
ReactDOM.render(<PolyfilledComponent />, container);
ReactDOM.render(
<React.StrictMode>
<PolyfilledComponent />
</React.StrictMode>,
container,
);
});

it('should not warn about unsafe lifecycles within "strict" tree for polyfilled components', () => {
const {StrictMode} = React;

it('should not warn for components with polyfilled getSnapshotBeforeUpdate', () => {
class PolyfilledComponent extends React.Component {
state = {};
static getDerivedStateFromProps() {
getSnapshotBeforeUpdate() {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old version were these two tests identical?

Copy link
Contributor Author

@bvaughn bvaughn Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old version had two tests for polyfilled getDerivedStateFromProps - one with and one without StrictMode. I don't think the one without is all that useful (since it's a subset) so I removed it. I replaced it with a new test (with StrictMode) for polyfilled getSnapshotBeforeUpdate.

}
componentDidUpdate() {}
render() {
return null;
}
Expand All @@ -99,9 +102,9 @@ describe('ReactComponentLifeCycle', () => {

const container = document.createElement('div');
ReactDOM.render(
<StrictMode>
<React.StrictMode>
<PolyfilledComponent />
</StrictMode>,
</React.StrictMode>,
container,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ describe('ReactDOMServerLifecycles', () => {
});

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

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

polyfill(PolyfilledComponent);

ReactDOMServer.renderToString(<PolyfilledComponent />);
const container = document.createElement('div');
ReactDOMServer.renderToString(
<React.StrictMode>
<PolyfilledComponent />
</React.StrictMode>,
container,
);
});

it('should not warn for components with polyfilled getSnapshotBeforeUpdate', () => {
class PolyfilledComponent extends React.Component {
getSnapshotBeforeUpdate() {
return null;
}
componentDidUpdate() {}
render() {
return null;
}
}

polyfill(PolyfilledComponent);

const container = document.createElement('div');
ReactDOMServer.renderToString(
<React.StrictMode>
<PolyfilledComponent />
</React.StrictMode>,
container,
);
});
});
});
20 changes: 7 additions & 13 deletions packages/react-reconciler/src/ReactStrictModeWarnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,6 @@ if (__DEV__) {
return;
}

// Don't warn about react-lifecycles-compat polyfilled components.
// Note that it is sufficient to check for the presence of a
// single lifecycle, componentWillMount, with the polyfill flag.
if (
typeof instance.componentWillMount === 'function' &&
instance.componentWillMount.__suppressDeprecationWarning === true
) {
return;
}

let warningsForRoot;
if (!pendingUnsafeLifecycleWarnings.has(strictRoot)) {
warningsForRoot = {
Expand All @@ -261,19 +251,23 @@ if (__DEV__) {

const unsafeLifecycles = [];
if (
typeof instance.componentWillMount === 'function' ||
(typeof instance.componentWillMount === 'function' &&
instance.componentWillMount.__suppressDeprecationWarning !== true) ||
typeof instance.UNSAFE_componentWillMount === 'function'
) {
unsafeLifecycles.push('UNSAFE_componentWillMount');
}
if (
typeof instance.componentWillReceiveProps === 'function' ||
(typeof instance.componentWillReceiveProps === 'function' &&
instance.componentWillReceiveProps.__suppressDeprecationWarning !==
true) ||
typeof instance.UNSAFE_componentWillReceiveProps === 'function'
) {
unsafeLifecycles.push('UNSAFE_componentWillReceiveProps');
}
if (
typeof instance.componentWillUpdate === 'function' ||
(typeof instance.componentWillUpdate === 'function' &&
instance.componentWillUpdate.__suppressDeprecationWarning !== true) ||
typeof instance.UNSAFE_componentWillUpdate === 'function'
) {
unsafeLifecycles.push('UNSAFE_componentWillUpdate');
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4434,9 +4434,9 @@ react-dom@15.5.4:
object-assign "^4.1.0"
prop-types "~15.5.7"

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

react@15.5.4:
version "15.5.4"
Expand Down