-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Conversation
@@ -365,7 +386,11 @@ describe('ReactFunctionComponent', () => { | |||
); | |||
}); | |||
|
|||
// TODO: deprecate default props support after we remove defaultProps | |||
// from function components |
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.
Didn't we plan to keep them for classes forever?
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.
Oh sorry, poor phrasing. I just meant that I need to change this test at some point. Will rephrase!
@@ -25,6 +26,8 @@ describe('ReactFunctionComponent', () => { | |||
React = require('react'); | |||
ReactDOM = require('react-dom'); | |||
ReactTestUtils = require('react-dom/test-utils'); | |||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | |||
ReactFeatureFlags.warnAbouDefaultPropsOnFunctionComponents = true; |
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.
s/warnAbouDefaultPropsOnFunctionComponents
/warnAboutDefaultPropsOnFunctionComponents
(note the extra t at the end of About
)
@@ -25,6 +26,8 @@ describe('ReactFunctionComponent', () => { | |||
React = require('react'); | |||
ReactDOM = require('react-dom'); | |||
ReactTestUtils = require('react-dom/test-utils'); | |||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | |||
ReactFeatureFlags.warnAbouDefaultPropsOnFunctionComponents = true; | |||
}); | |||
|
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.
afterEach(() => { | |
ReactFeatureFlags.warnAboutDefaultPropsOnFunctionComponents = false; | |
}) |
expect(() => | ||
ReactTestUtils.renderIntoDocument(<FunctionalComponent />), | ||
).toWarnDev( | ||
'Warning: FunctionalComponent: Function components do not support defaultProps. ' + |
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.
do not, or will not in a future version?
warningWithoutStack( | ||
false, | ||
'%s: Function components do not support defaultProps. ' + | ||
'Use Javascript default arguments instead.', |
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.
'Use Javascript default arguments instead.', | |
'Use JavaScript default arguments instead.', |
ReactTestUtils.renderIntoDocument(<FunctionalComponent />), | ||
).toWarnDev( | ||
'Warning: FunctionalComponent: Function components do not support defaultProps. ' + | ||
'Use Javascript default arguments instead.', |
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.
'Use Javascript default arguments instead.', | |
'Use JavaScript default arguments instead.', |
Tests suites that modify feature flags need an The reason is that we have a CI job that runs our unit tests against the actual npm packages. |
Need to update all the feature flag files to include the new one you added. E.g. |
…eactFeatureFlag files to include new flag
ReactDOM: size: 0.0%, gzip: -0.0% Details of bundled changes.Comparing: 121bfb0...9c7d282 react-art
react-dom
react-test-renderer
react-reconciler
react-native-renderer
Generated by 🚫 dangerJS |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth mentioning it's ES2015 feature ... or not. 🤷♂
ReactTestUtils.renderIntoDocument(<FunctionalComponent />), | ||
).toWarnDev( | ||
'Warning: FunctionalComponent: Function components will not support defaultProps. ' + | ||
'Use JavaScript default arguments instead.', |
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
Replace usage of defaultProps. This is deprecated and will be removed in future React versions (see facebook/react#16210)
As part of the process to deprecate defaultProps on function components (as per a larger proposal outlined here), add a warning whenever someone does this.