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

Bug: React fails to log invariant 306 message when lazy() resolves to a Fragment #26910

Closed
tom-sherman opened this issue Jun 7, 2023 · 7 comments · Fixed by #30372
Closed
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@tom-sherman
Copy link
Contributor

React version: 18.2.0

Steps To Reproduce

  1. Try to render this component: const LazyFragment = lazy(() => Promise.resolve({ default: Fragment }));

Link to code example:

https://github.com/tom-sherman/react-lazy-fragment-bug

The current behavior

It throws an error:

Cannot convert a Symbol value to a string

The expected behavior

It should throw an error related to invariant 306 eg.

Element type is invalid. Received a promise that resolves to: Fragment. Lazy element type must resolve to a class or function.
@tom-sherman tom-sherman added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jun 7, 2023
@MeenuyD
Copy link

MeenuyD commented Jun 7, 2023

Hello, I think the issue you're encountering is related to using lazy with a dynamically resolved promise that resolves to { default: Fragment }. However, lazy expects the resolved value to be a class or function component, not an object.
use dynamic import to load the Fragment component from the react module, and then we wrap it in an object with the default property as { default: module.Fragment }. This ensures that lazy receives a valid component as its resolved value.

@tom-sherman
Copy link
Contributor Author

tom-sherman commented Jun 7, 2023

Firstly {default: Fragment} is not the issue. Lazy doesn't care about immediately resolved promises, this pattern works fine with function components or classes.

The same issue happens if you default export Fragment from a module and then lazy(() => import("./LazyFragment")). See tom-sherman/react-lazy-fragment-bug@95b1567

Secondly, the expected behaviour is to log an appropriate error from React. Instead it's failing to do so and throwing a random exception.

Whether a lazily loaded Fragment should work is besides the point, that can be discussed in a separate issue. This bug is about the incorrect error message being displayed.

Copy link

GitHub Actions bot commented Apr 9, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions GitHub Actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 9, 2024
@tom-sherman
Copy link
Contributor Author

Still an issue on latest canary.

@github-actions GitHub Actions bot removed the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

GitHub Actions bot commented Jul 9, 2024

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions GitHub Actions bot added the Resolution: Stale Automatically closed due to inactivity label Jul 9, 2024
Copy link

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@github-actions GitHub Actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2024
@tom-sherman
Copy link
Contributor Author

This still exists on latest canary.

@eps1lon eps1lon reopened this Jul 18, 2024
@github-actions GitHub Actions bot removed the Resolution: Stale Automatically closed due to inactivity label Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants