Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import {
REACT_FORWARD_REF_TYPE,
REACT_MEMO_TYPE,
getIteratorFn,
REACT_FRAGMENT_TYPE,
} from 'shared/ReactSymbols';
import {
getCurrentFiberOwnerNameInDevOrNull,
Expand Down Expand Up @@ -1891,11 +1892,13 @@ function mountLazyComponent(
}
}

const loggedComponent = Component === REACT_FRAGMENT_TYPE ? '<Fragment>' : Component;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can use getComponentNameFromType from shared/getComponentNameFromType here instead. That way we also get a better name for all the other types you're not supposed to use.

Suggested change
const loggedComponent = Component === REACT_FRAGMENT_TYPE ? '<Fragment>' : Component;
const loggedComponent = getComponentNameFromType(Component) || String(Component);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Forgot that we use + '' for perf so || (Component + '') it is.


// 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}`,
);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>. ' +
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
'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));
Expand Down