-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Warn when Using DefaultProps on Function Components #16210
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39fee28
add default props test
lunaruan d72dbbd
add code to warn when using default props on function component
lunaruan e3afc8c
Merge branch 'master' of https://github.com/facebook/react into depre…
lunaruan e3abfa4
clean up console.logs/random spaces
lunaruan a6fd7d5
one more console.log
lunaruan 2395810
fixed spelling/wording, moved test to new file, and updated all the R…
lunaruan 079d17c
changed wording
lunaruan 9c7d282
changed warning wording
lunaruan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/react-dom/src/__tests__/ReactDeprecationWarnings-test.internal.js
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,46 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* 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 ReactTestUtils; | ||
let ReactFeatureFlags; | ||
|
||
describe('ReactDeprecationWarnings', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactTestUtils = require('react-dom/test-utils'); | ||
ReactFeatureFlags.warnAboutDefaultPropsOnFunctionComponents = true; | ||
}); | ||
|
||
afterEach(() => { | ||
ReactFeatureFlags.warnAboutDefaultPropsOnFunctionComponents = false; | ||
}); | ||
|
||
it('should warn when given defaultProps', () => { | ||
function FunctionalComponent(props) { | ||
return null; | ||
} | ||
|
||
FunctionalComponent.defaultProps = { | ||
testProp: true, | ||
}; | ||
|
||
expect(() => | ||
ReactTestUtils.renderIntoDocument(<FunctionalComponent />), | ||
).toWarnDev( | ||
'Warning: FunctionalComponent: Function components will not support defaultProps. ' + | ||
'Use JavaScript default arguments instead.', | ||
{withoutStack: true}, | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -62,6 +62,7 @@ import { | |
enableSuspenseServerRenderer, | ||
enableFlareAPI, | ||
enableFundamentalAPI, | ||
warnAboutDefaultPropsOnFunctionComponents, | ||
} from 'shared/ReactFeatureFlags'; | ||
import invariant from 'shared/invariant'; | ||
import shallowEqual from 'shared/shallowEqual'; | ||
|
@@ -187,6 +188,7 @@ export let didWarnAboutReassigningProps; | |
let didWarnAboutMaxDuration; | ||
let didWarnAboutRevealOrder; | ||
let didWarnAboutTailOptions; | ||
let didWarnAboutDefaultPropsOnFunctionComponent; | ||
|
||
if (__DEV__) { | ||
didWarnAboutBadClass = {}; | ||
|
@@ -198,6 +200,7 @@ if (__DEV__) { | |
didWarnAboutMaxDuration = false; | ||
didWarnAboutRevealOrder = {}; | ||
didWarnAboutTailOptions = {}; | ||
didWarnAboutDefaultPropsOnFunctionComponent = {}; | ||
} | ||
|
||
export function reconcileChildren( | ||
|
@@ -1424,6 +1427,23 @@ function validateFunctionComponentInDev(workInProgress: Fiber, Component: any) { | |
} | ||
} | ||
|
||
if ( | ||
warnAboutDefaultPropsOnFunctionComponents && | ||
Component.defaultProps !== undefined | ||
) { | ||
const componentName = getComponentName(Component) || 'Unknown'; | ||
|
||
if (!didWarnAboutDefaultPropsOnFunctionComponent[componentName]) { | ||
warningWithoutStack( | ||
false, | ||
'%s: Function components will not support defaultProps. ' + | ||
'Use JavaScript default arguments instead.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth mentioning it's ES2015 feature ... or not. 🤷♂ |
||
componentName, | ||
); | ||
didWarnAboutDefaultPropsOnFunctionComponent[componentName] = true; | ||
} | ||
} | ||
|
||
if (typeof Component.getDerivedStateFromProps === 'function') { | ||
const componentName = getComponentName(Component) || 'Unknown'; | ||
|
||
|
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alt suggestion for first sentence: "Support for
defaultProps
will be removed from function components in a future release."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally, a future major release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I assumed that was implied but should probably be specific