Warn about setting propTypes too

This commit is contained in:
Dan Abramov
2018-11-21 16:16:06 +00:00
parent 1c64922e3f
commit 9111508981
2 changed files with 27 additions and 0 deletions
@@ -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');
+15
View File
@@ -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;
},
},
});
}