-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RFC 6: Deprecate unsafe lifecycles (#12028)
* Added unsafe_* lifecycles and deprecation warnings If the old lifecycle hooks (componentWillMount, componentWillUpdate, componentWillReceiveProps) are detected, these methods will be called and a deprecation warning will be logged. (In other words, we do not check for both the presence of the old and new lifecycles.) This commit is expected to fail tests. * Ran lifecycle hook codemod over project This should handle the bulk of the updates. I will manually update TypeScript and CoffeeScript tests with another commit. The actual command run with this commit was: jscodeshift --parser=flow -t ../react-codemod/transforms/rename-unsafe-lifecycles.js ./packages/**/src/**/*.js * Manually migrated CoffeeScript and TypeScript tests * Added inline note to createReactClassIntegration-test Explaining why lifecycles hooks have not been renamed in this test. * Udated NativeMethodsMixin with new lifecycle hooks * Added static getDerivedStateFromProps to ReactPartialRenderer Also added a new set of tests focused on server side lifecycle hooks. * Added getDerivedStateFromProps to shallow renderer Also added warnings for several cases involving getDerivedStateFromProps() as well as the deprecated lifecycles. Also added tests for the above. * Dedupe and DEV-only deprecation warning in server renderer * Renamed unsafe_* prefix to UNSAFE_* to be more noticeable * Added getDerivedStateFromProps to ReactFiberClassComponent Also updated class component and lifecyle tests to cover the added functionality. * Warn about UNSAFE_componentWillRecieveProps misspelling * Added tests to createReactClassIntegration for new lifecycles * Added warning for stateless functional components with gDSFP * Added createReactClass test for static gDSFP * 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. * Tidying up * Tweaked warning message wording slightly Replaced 'You may may have returned undefined.' with 'You may have returned undefined.' * Replaced truthy partialState checks with != null * Call getDerivedStateFromProps via .call(null) to prevent type access * Move shallow-renderer didWarn* maps off the instance * Only call getDerivedStateFromProps if props instance has changed * Avoid creating new state object if not necessary * Inject state as a param to callGetDerivedStateFromProps This value will be either workInProgress.memoizedState (for updates) or instance.state (for initialization). * Explicitly warn about uninitialized state before calling getDerivedStateFromProps. And added some new tests for this change. Also: * Improved a couple of falsy null/undefined checks to more explicitly check for null or undefined. * Made some small tweaks to ReactFiberClassComponent WRT when and how it reads instance.state and sets to null. * Improved wording for deprecation lifecycle warnings * Fix state-regression for module-pattern components Also add support for new static getDerivedStateFromProps method
- Loading branch information
Showing
3 changed files
with
406 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* 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 | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let createRenderer; | ||
let React; | ||
let ReactFeatureFlags; | ||
|
||
describe('ReactShallowRenderer', () => { | ||
beforeEach(() => { | ||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.warnAboutDeprecatedLifecycles = true; | ||
|
||
createRenderer = require('react-test-renderer/shallow').createRenderer; | ||
React = require('react'); | ||
}); | ||
|
||
// TODO (RFC #6) Merge this back into ReactShallowRenderer-test once | ||
// the 'warnAboutDeprecatedLifecycles' feature flag has been removed. | ||
it('should warn if deprecated lifecycles exist', () => { | ||
class ComponentWithWarnings extends React.Component { | ||
componentWillReceiveProps() {} | ||
componentWillMount() {} | ||
componentWillUpdate() {} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
const shallowRenderer = createRenderer(); | ||
expect(() => shallowRenderer.render(<ComponentWithWarnings />)).toWarnDev( | ||
'Warning: ComponentWithWarnings: componentWillMount() is deprecated and will ' + | ||
'be removed in the next major version.', | ||
); | ||
expect(() => shallowRenderer.render(<ComponentWithWarnings />)).toWarnDev([ | ||
'Warning: ComponentWithWarnings: componentWillReceiveProps() is deprecated ' + | ||
'and will be removed in the next major version.', | ||
'Warning: ComponentWithWarnings: componentWillUpdate() is deprecated and will ' + | ||
'be removed in the next major version.', | ||
]); | ||
|
||
// Verify no duplicate warnings | ||
shallowRenderer.render(<ComponentWithWarnings />); | ||
}); | ||
}); |
Oops, something went wrong.