mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warn about setting propTypes too
This commit is contained in:
@@ -510,6 +510,18 @@ describe('ReactLazy', () => {
|
||||
expect(root).toMatchRenderedOutput('Friends Bye');
|
||||
});
|
||||
|
||||
it('warns about defining propTypes on the outer wrapper', async () => {
|
||||
const LazyText = lazy(() => fakeImport(Text));
|
||||
expect(() => {
|
||||
LazyText.propTypes = {hello: () => {}};
|
||||
}).toWarnDev(
|
||||
'React.lazy(...): It is not supported to assign `propTypes` to ' +
|
||||
'a lazy component import. Either specify them where the component ' +
|
||||
'is defined, or create a wrapping component around it.',
|
||||
{withoutStack: true},
|
||||
);
|
||||
});
|
||||
|
||||
it('includes lazy-loaded component in warning stack', async () => {
|
||||
const LazyFoo = lazy(() => {
|
||||
ReactTestRenderer.unstable_yield('Started loading');
|
||||
|
||||
@@ -22,6 +22,7 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
|
||||
if (__DEV__) {
|
||||
// In production, this would just set it on the object.
|
||||
let defaultProps;
|
||||
let propTypes;
|
||||
Object.defineProperties(lazyType, {
|
||||
defaultProps: {
|
||||
get() {
|
||||
@@ -37,6 +38,20 @@ export function lazy<T, R>(ctor: () => Thenable<T, R>): LazyComponent<T> {
|
||||
defaultProps = newDefaultProps;
|
||||
},
|
||||
},
|
||||
propTypes: {
|
||||
get() {
|
||||
return propTypes;
|
||||
},
|
||||
set(newPropTypes) {
|
||||
warning(
|
||||
false,
|
||||
'React.lazy(...): It is not supported to assign `propTypes` to ' +
|
||||
'a lazy component import. Either specify them where the component ' +
|
||||
'is defined, or create a wrapping component around it.',
|
||||
);
|
||||
propTypes = newPropTypes;
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user