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

More useful DEV error message when lazy-rendering Context Provider #27168

Closed

Conversation

markspolakovs
Copy link
Contributor

@markspolakovs markspolakovs commented Jul 29, 2023

Summary

If you render a createContext().Provider from a lazy(), the error message is difficult to understand:

Error: Element type is invalid. Received a promise that resolves to: [object Object]. Lazy element type must resolve to a class or function.

This PR adds a DEV-time hint, following the pattern of the existing one for double-lazy():

Error: Element type is invalid. Received a promise that resolves to: [object Object]. Lazy element type must resolve to a class or function. Context Providers cannot be lazily rendered without being wrapped in a component.

How did you test this change?

Added a new test and ran yarn test.

@react-sizebot
Copy link

react-sizebot commented Jul 29, 2023

Comparing: 98f3f14...4ad8f61

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 165.67 kB 165.67 kB = 51.99 kB 51.99 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 173.16 kB 173.16 kB = 54.21 kB 54.21 kB
facebook-www/ReactDOM-prod.classic.js = 570.50 kB 570.50 kB = 100.49 kB 100.49 kB
facebook-www/ReactDOM-prod.modern.js = 554.30 kB 554.30 kB = 97.66 kB 97.66 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 4ad8f61

@markspolakovs markspolakovs force-pushed the marks/lazy-provider-error branch 2 times, most recently from 94fa71d to 526a39f Compare July 29, 2023 12:06
Copy link

@yuvvrajs yuvvrajs left a comment

Choose a reason for hiding this comment

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

Demo.

@markspolakovs markspolakovs force-pushed the marks/lazy-provider-error branch 3 times, most recently from 4bdb8eb to 65e292f Compare August 15, 2023 09:45
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

Copy link

github-actions bot commented Apr 8, 2024

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 8, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants