Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ import shallowEqual from 'shared/shallowEqual';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import ReactStrictModeWarnings from './ReactStrictModeWarnings';
import {REACT_LAZY_TYPE, getIteratorFn} from 'shared/ReactSymbols';
import {
REACT_LAZY_TYPE,
REACT_PROVIDER_TYPE,
getIteratorFn,
} from 'shared/ReactSymbols';
import {
getCurrentFiberOwnerNameInDevOrNull,
setIsRendering,
Expand Down Expand Up @@ -1831,12 +1835,13 @@ function mountLazyComponent(
}
let hint = '';
if (__DEV__) {
if (
Component !== null &&
typeof Component === 'object' &&
Component.$$typeof === REACT_LAZY_TYPE
) {
hint = ' Did you wrap a component in React.lazy() more than once?';
if (Component !== null && typeof Component === 'object') {
if (Component.$$typeof === REACT_LAZY_TYPE) {
hint = ' Did you wrap a component in React.lazy() more than once?';
} else if (Component.type.$$typeof === REACT_PROVIDER_TYPE) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (Component.type.$$typeof === REACT_PROVIDER_TYPE) {
} else if (Component.type?.$$typeof === REACT_PROVIDER_TYPE) {

Defensive optional chaining in case Component.type doesn't exist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to fail the tests

hint =
' Context Providers cannot be lazily rendered without being wrapped in a component.';
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,37 @@ describe('ReactLazy', () => {
);
});

it('throws with a useful error when rendering a context Provider', async () => {
const ctx = React.createContext(null);
const Provider = <ctx.Provider value={{}} />;
const BadLazy = lazy(() => fakeImport(Provider));

const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<BadLazy />
</Suspense>,
{
unstable_isConcurrent: true,
},
);

await waitForAll(['Loading...']);

await resolveFakeImport(Provider);
root.update(
<Suspense fallback={<Text text="Loading..." />}>
<BadLazy />
</Suspense>,
);
await waitForThrow(
'Element type is invalid. Received a promise that resolves to: [object Object]. ' +
'Lazy element type must resolve to a class or function.' +
(__DEV__
? ' Context Providers cannot be lazily rendered without being wrapped in a component.'
: ''),
);
});

it('warns about defining propTypes on the outer wrapper', () => {
const LazyText = lazy(() => fakeImport(Text));
expect(() => {
Expand Down