Skip to content

Commit 9cc0f6e

Browse files
authored
Log Fragment name when trying to render a lazy fragment (#30372)
1 parent f6cce07 commit 9cc0f6e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1891,11 +1891,13 @@ function mountLazyComponent(
18911891
}
18921892
}
18931893

1894+
const loggedComponent = getComponentNameFromType(Component) || Component;
1895+
18941896
// This message intentionally doesn't mention ForwardRef or MemoComponent
18951897
// because the fact that it's a separate type of work is an
18961898
// implementation detail.
18971899
throw new Error(
1898-
`Element type is invalid. Received a promise that resolves to: ${Component}. ` +
1900+
`Element type is invalid. Received a promise that resolves to: ${loggedComponent}. ` +
18991901
`Lazy element type must resolve to a class or function.${hint}`,
19001902
);
19011903
}

packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js

+26
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,32 @@ describe('ReactLazy', () => {
757757
);
758758
});
759759

760+
it('throws with a useful error when wrapping fragment with lazy()', async () => {
761+
const BadLazy = lazy(() => fakeImport(React.Fragment));
762+
763+
const root = ReactTestRenderer.create(
764+
<Suspense fallback={<Text text="Loading..." />}>
765+
<BadLazy />
766+
</Suspense>,
767+
{
768+
unstable_isConcurrent: true,
769+
},
770+
);
771+
772+
await waitForAll(['Loading...']);
773+
774+
await resolveFakeImport(React.Fragment);
775+
root.update(
776+
<Suspense fallback={<Text text="Loading..." />}>
777+
<BadLazy />
778+
</Suspense>,
779+
);
780+
await waitForThrow(
781+
'Element type is invalid. Received a promise that resolves to: Fragment. ' +
782+
'Lazy element type must resolve to a class or function.',
783+
);
784+
});
785+
760786
it('throws with a useful error when wrapping lazy() multiple times', async () => {
761787
const Lazy1 = lazy(() => fakeImport(Text));
762788
const Lazy2 = lazy(() => fakeImport(Lazy1));

0 commit comments

Comments
 (0)