From 9111508981ff84b185726f0a4e018de276896af8 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 21 Nov 2018 16:16:06 +0000 Subject: [PATCH] Warn about setting propTypes too --- .../src/__tests__/ReactLazy-test.internal.js | 12 ++++++++++++ packages/react/src/ReactLazy.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js b/packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js index fe3ec703f1..2339abed9b 100644 --- a/packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js @@ -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'); diff --git a/packages/react/src/ReactLazy.js b/packages/react/src/ReactLazy.js index 1755cb671c..947b435471 100644 --- a/packages/react/src/ReactLazy.js +++ b/packages/react/src/ReactLazy.js @@ -22,6 +22,7 @@ export function lazy(ctor: () => Thenable): LazyComponent { 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(ctor: () => Thenable): LazyComponent { 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; + }, + }, }); }