Log Fragment name when trying to render a lazy fragment (#30372)

This commit is contained in:
Tom Sherman
2024-07-23 19:00:51 +02:00
committed by GitHub
parent f6cce072cf
commit 9cc0f6e68d
2 changed files with 29 additions and 1 deletions
+3 -1
View File
@@ -1891,11 +1891,13 @@ function mountLazyComponent(
}
}
const loggedComponent = getComponentNameFromType(Component) || Component;
// This message intentionally doesn't mention ForwardRef or MemoComponent
// because the fact that it's a separate type of work is an
// implementation detail.
throw new Error(
`Element type is invalid. Received a promise that resolves to: ${Component}. ` +
`Element type is invalid. Received a promise that resolves to: ${loggedComponent}. ` +
`Lazy element type must resolve to a class or function.${hint}`,
);
}
@@ -757,6 +757,32 @@ describe('ReactLazy', () => {
);
});
it('throws with a useful error when wrapping fragment with lazy()', async () => {
const BadLazy = lazy(() => fakeImport(React.Fragment));
const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<BadLazy />
</Suspense>,
{
unstable_isConcurrent: true,
},
);
await waitForAll(['Loading...']);
await resolveFakeImport(React.Fragment);
root.update(
<Suspense fallback={<Text text="Loading..." />}>
<BadLazy />
</Suspense>,
);
await waitForThrow(
'Element type is invalid. Received a promise that resolves to: Fragment. ' +
'Lazy element type must resolve to a class or function.',
);
});
it('throws with a useful error when wrapping lazy() multiple times', async () => {
const Lazy1 = lazy(() => fakeImport(Text));
const Lazy2 = lazy(() => fakeImport(Lazy1));