Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e709e30
Added unsafe_* lifecycles and deprecation warnings
bvaughn Jan 16, 2018
404944e
Ran lifecycle hook codemod over project
bvaughn Jan 16, 2018
bd300b3
Manually migrated CoffeeScript and TypeScript tests
bvaughn Jan 16, 2018
2868176
Added inline note to createReactClassIntegration-test
bvaughn Jan 16, 2018
8679926
Udated NativeMethodsMixin with new lifecycle hooks
bvaughn Jan 16, 2018
64f27d7
Added static getDerivedStateFromProps to ReactPartialRenderer
bvaughn Jan 17, 2018
1047182
Added getDerivedStateFromProps to shallow renderer
bvaughn Jan 17, 2018
035c220
Dedupe and DEV-only deprecation warning in server renderer
bvaughn Jan 17, 2018
8d0e001
Renamed unsafe_* prefix to UNSAFE_* to be more noticeable
bvaughn Jan 17, 2018
b71ca93
Added getDerivedStateFromProps to ReactFiberClassComponent
bvaughn Jan 17, 2018
09c39d0
Warn about UNSAFE_componentWillRecieveProps misspelling
bvaughn Jan 17, 2018
286df77
Added tests to createReactClassIntegration for new lifecycles
bvaughn Jan 17, 2018
b699543
Added warning for stateless functional components with gDSFP
bvaughn Jan 17, 2018
68f2fe7
Added createReactClass test for static gDSFP
bvaughn Jan 18, 2018
2d9f75d
Moved lifecycle deprecation warnings behind (disabled) feature flag
bvaughn Jan 18, 2018
8f125b7
Tidying up
bvaughn Jan 18, 2018
1d3e3d5
Merge branch 'master' into rfc-6
bvaughn Jan 18, 2018
d95ec49
Tweaked warning message wording slightly
bvaughn Jan 18, 2018
b940938
Replaced truthy partialState checks with != null
bvaughn Jan 18, 2018
6cd0a8e
Call getDerivedStateFromProps via .call(null) to prevent type access
bvaughn Jan 18, 2018
7572667
Move shallow-renderer didWarn* maps off the instance
bvaughn Jan 18, 2018
361a2cf
Only call getDerivedStateFromProps if props instance has changed
bvaughn Jan 18, 2018
8178d52
Avoid creating new state object if not necessary
bvaughn Jan 18, 2018
8d67e27
Inject state as a param to callGetDerivedStateFromProps
bvaughn Jan 18, 2018
53770c3
Explicitly warn about uninitialized state before calling getDerivedSt…
bvaughn Jan 18, 2018
3772ee2
Improved wording for deprecation lifecycle warnings
bvaughn Jan 18, 2018
4dfa6e1
Merge branch 'master' into rfc-6
bvaughn Jan 18, 2018
5609031
Fix state-regression for module-pattern components
bvaughn Jan 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moved lifecycle deprecation warnings behind (disabled) feature flag
Updated tests accordingly, by temporarily splitting tests that were specific to this feature-flag into their own, internal tests. This was the only way I knew of to interact with the feature flag without breaking our build/dist tests.
  • Loading branch information
bvaughn committed Jan 18, 2018
commit 2d9f75d31dba3de2072ce362bfdd5af28b053167
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

let React;
let ReactDOM;
let ReactFeatureFlags;

/**
* TODO: We should make any setState calls fail in
* `getInitialState` and `componentWillMount`. They will usually fail
* anyways because `this._renderedComponent` is empty, however, if a component
* is *reused*, then that won't be the case and things will appear to work in
* some cases. Better to just block all updates in initialization.
*/
describe('ReactComponentLifeCycle', () => {
beforeEach(() => {
jest.resetModules();

ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.warnAboutDeprecatedLifecycles = true;

React = require('react');
ReactDOM = require('react-dom');
});

// TODO (RFC #6) Merge this back into ReactComponentLifeCycles-test once
// the 'warnAboutDeprecatedLifecycles' feature flag has been removed.
it('warns about deprecated unsafe lifecycles', function() {
class MyComponent extends React.Component {
componentWillMount() {}
componentWillReceiveProps() {}
componentWillUpdate() {}
render() {
return null;
}
}

const container = document.createElement('div');
expect(() => ReactDOM.render(<MyComponent x={1} />, container)).toWarnDev([
'Warning: MyComponent: componentWillMount() is deprecated and will be ' +
'removed in the next major version. ' +
'Please use UNSAFE_componentWillMount() instead.',
]);
Copy link
Collaborator

@acdlite acdlite Jan 18, 2018

Choose a reason for hiding this comment

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

How about something like this?

Warning: MyComponent: componentWillMount() is deprecated and will be removed in the next major version. Read about the motivations behind this change: (link)

As a temporary workaround, you can rename to UNSAFE_componentWillMount instead.

(link) can point to some fburl that we write later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For now the link can be your RFC :)


expect(() => ReactDOM.render(<MyComponent x={2} />, container)).toWarnDev([
'Warning: MyComponent: componentWillReceiveProps() is deprecated and ' +
'will be removed in the next major version. ' +
'Please use UNSAFE_componentWillReceiveProps() instead.',
Copy link
Collaborator

@acdlite acdlite Jan 18, 2018

Choose a reason for hiding this comment

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

For this one, we should call out getDerivedStateFromProps as the preferred solution.

Warning: MyComponent: componentWillReceiveProps() is deprecated and will be removed in the next major version. Use getDerivedStateFromProps() instead. Read about the motivations behind this change: (link)

As a temporary workaround, you can rename to UNSAFE_componentWillReceiveProps instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! Thanks for the improved wording suggestions!

I've made the wording improvements in 3772ee2.

'Warning: MyComponent: componentWillUpdate() is deprecated and will be ' +
'removed in the next major version. ' +
'Please use UNSAFE_componentWillUpdate() instead.',
]);

// Dedupe check (instantiate and update)
ReactDOM.render(<MyComponent key="new" x={1} />, container);
ReactDOM.render(<MyComponent key="new" x={2} />, container);
});
});
31 changes: 0 additions & 31 deletions packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,37 +593,6 @@ describe('ReactComponentLifeCycle', () => {
]);
});

it('warns about deprecated unsafe lifecycles', function() {
class MyComponent extends React.Component {
componentWillMount() {}
componentWillReceiveProps() {}
componentWillUpdate() {}
render() {
return null;
}
}

const container = document.createElement('div');
expect(() => ReactDOM.render(<MyComponent x={1} />, container)).toWarnDev([
'Warning: MyComponent: componentWillMount() is deprecated and will be ' +
'removed in the next major version. ' +
'Please use UNSAFE_componentWillMount() instead.',
]);

expect(() => ReactDOM.render(<MyComponent x={2} />, container)).toWarnDev([
'Warning: MyComponent: componentWillReceiveProps() is deprecated and ' +
'will be removed in the next major version. ' +
'Please use UNSAFE_componentWillReceiveProps() instead.',
'Warning: MyComponent: componentWillUpdate() is deprecated and will be ' +
'removed in the next major version. ' +
'Please use UNSAFE_componentWillUpdate() instead.',
]);

// Dedupe check (instantiate and update)
ReactDOM.render(<MyComponent key="new" x={1} />, container);
ReactDOM.render(<MyComponent key="new" x={2} />, container);
});

it('calls effects on module-pattern component', function() {
const log = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

let React;
let ReactFeatureFlags;
let ReactDOMServer;

describe('ReactDOMServerLifecycles', () => {
beforeEach(() => {
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.warnAboutDeprecatedLifecycles = true;

React = require('react');
ReactDOMServer = require('react-dom/server');
});

// TODO (RFC #6) Merge this back into ReactDOMServerLifecycles-test once
// the 'warnAboutDeprecatedLifecycles' feature flag has been removed.
it('should warn about deprecated lifecycle hooks', () => {
class Component extends React.Component {
componentWillMount() {}
render() {
return null;
}
}

expect(() => ReactDOMServer.renderToString(<Component />)).toWarnDev(
'Warning: Component: componentWillMount() is deprecated and will be removed ' +
'in the next major version. Please use UNSAFE_componentWillMount() instead.',
);

// De-duped
ReactDOMServer.renderToString(<Component />);
});
});
17 changes: 0 additions & 17 deletions packages/react-dom/src/__tests__/ReactDOMServerLifecycles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,6 @@ describe('ReactDOMServerLifecycles', () => {
]);
});

it('should warn about deprecated lifecycle hooks', () => {
class Component extends React.Component {
componentWillMount() {}
render() {
return null;
}
}

expect(() => ReactDOMServer.renderToString(<Component />)).toWarnDev(
'Warning: Component: componentWillMount() is deprecated and will be removed ' +
'in the next major version. Please use UNSAFE_componentWillMount() instead.',
);

// De-duped
ReactDOMServer.renderToString(<Component />);
});

it('should update instance.state with value returned from getDerivedStateFromProps', () => {
class Grandparent extends React.Component {
state = {
Expand Down
21 changes: 12 additions & 9 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import warning from 'fbjs/lib/warning';
import checkPropTypes from 'prop-types/checkPropTypes';
import describeComponentFrame from 'shared/describeComponentFrame';
import {ReactDebugCurrentFrame} from 'shared/ReactGlobalSharedState';
import {warnAboutDeprecatedLifecycles} from 'shared/ReactFeatureFlags';
import {
REACT_FRAGMENT_TYPE,
REACT_CALL_TYPE,
Expand Down Expand Up @@ -489,16 +490,18 @@ function resolve(
if (inst.UNSAFE_componentWillMount || inst.componentWillMount) {
if (inst.componentWillMount) {
if (__DEV__) {
const componentName = getComponentName(Component) || 'Unknown';
if (warnAboutDeprecatedLifecycles) {
const componentName = getComponentName(Component) || 'Unknown';

if (!didWarnAboutDeprecatedWillMount[componentName]) {
warning(
false,
'%s: componentWillMount() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillMount() instead.',
componentName,
);
didWarnAboutDeprecatedWillMount[componentName] = true;
if (!didWarnAboutDeprecatedWillMount[componentName]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know they're not enabled yet but I don't think it's reasonable to emit one warning per component when we enable them.

I would probably coalesce them across components. Gather data during reconciliation and then print after commit:

The componentWillMount() hook has been deprecated [...] It is currently used by the following components: A, B, C, [...].

This doesn't block the PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Do we do this (coalescing warnings) anywhere else?

I will add this to the list of follow-up items.

Copy link
Collaborator

@gaearon gaearon Jan 18, 2018

Choose a reason for hiding this comment

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

Yes, we do (or, rather, did, since we removed the whitelist in 16) this for unknown DOM props. Before we did this, the users were super annoyed because we literally printed hundreds of warnings. I think we need to be very unobtrusive here because this change is already very churning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good follow up thing to do then. Thanks for the suggestion!

warning(
false,
'%s: componentWillMount() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillMount() instead.',
componentName,
);
didWarnAboutDeprecatedWillMount[componentName] = true;
}
}
}

Expand Down
71 changes: 40 additions & 31 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {Update} from 'shared/ReactTypeOfSideEffect';
import {
debugRenderPhaseSideEffects,
enableAsyncSubtreeAPI,
warnAboutDeprecatedLifecycles,
} from 'shared/ReactFeatureFlags';
import {isMounted} from 'react-reconciler/reflection';
import * as ReactInstanceMap from 'shared/ReactInstanceMap';
Expand Down Expand Up @@ -49,9 +50,11 @@ let didWarnAboutWillReceivePropsAndDerivedState;
let warnOnInvalidCallback;

if (__DEV__) {
didWarnAboutLegacyWillMount = {};
didWarnAboutLegacyWillReceiveProps = {};
didWarnAboutLegacyWillUpdate = {};
if (warnAboutDeprecatedLifecycles) {
didWarnAboutLegacyWillMount = {};
didWarnAboutLegacyWillReceiveProps = {};
didWarnAboutLegacyWillUpdate = {};
}
didWarnAboutStateAssignmentForComponent = {};
didWarnAboutUndefinedDerivedState = {};
didWarnAboutWillReceivePropsAndDerivedState = {};
Expand Down Expand Up @@ -426,15 +429,17 @@ export default function(

if (typeof instance.componentWillMount === 'function') {
if (__DEV__) {
const componentName = getComponentName(workInProgress) || 'Unknown';
if (!didWarnAboutLegacyWillMount[componentName]) {
warning(
false,
'%s: componentWillMount() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillMount() instead.',
componentName,
);
didWarnAboutLegacyWillMount[componentName] = true;
if (warnAboutDeprecatedLifecycles) {
const componentName = getComponentName(workInProgress) || 'Unknown';
if (!didWarnAboutLegacyWillMount[componentName]) {
warning(
false,
'%s: componentWillMount() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillMount() instead.',
componentName,
);
didWarnAboutLegacyWillMount[componentName] = true;
}
}
}
instance.componentWillMount();
Expand Down Expand Up @@ -467,15 +472,17 @@ export default function(
const oldState = instance.state;
if (typeof instance.componentWillReceiveProps === 'function') {
if (__DEV__) {
const componentName = getComponentName(workInProgress) || 'Unknown';
if (!didWarnAboutLegacyWillReceiveProps[componentName]) {
warning(
false,
'%s: componentWillReceiveProps() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillReceiveProps() instead.',
componentName,
);
didWarnAboutLegacyWillReceiveProps[componentName] = true;
if (warnAboutDeprecatedLifecycles) {
const componentName = getComponentName(workInProgress) || 'Unknown';
if (!didWarnAboutLegacyWillReceiveProps[componentName]) {
warning(
false,
'%s: componentWillReceiveProps() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillReceiveProps() instead.',
componentName,
);
didWarnAboutLegacyWillReceiveProps[componentName] = true;
}
}
}

Expand Down Expand Up @@ -814,16 +821,18 @@ export default function(
) {
if (typeof instance.componentWillUpdate === 'function') {
if (__DEV__) {
const componentName =
getComponentName(workInProgress) || 'Component';
if (!didWarnAboutLegacyWillUpdate[componentName]) {
warning(
false,
'%s: componentWillUpdate() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillUpdate() instead.',
componentName,
);
didWarnAboutLegacyWillUpdate[componentName] = true;
if (warnAboutDeprecatedLifecycles) {
const componentName =
getComponentName(workInProgress) || 'Component';
if (!didWarnAboutLegacyWillUpdate[componentName]) {
warning(
false,
'%s: componentWillUpdate() is deprecated and will be removed in the ' +
'next major version. Please use UNSAFE_componentWillUpdate() instead.',
componentName,
);
didWarnAboutLegacyWillUpdate[componentName] = true;
}
}
}

Expand Down
Loading