-
Notifications
You must be signed in to change notification settings - Fork 47.8k
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
Refactor Lazy Components to use teh Suspense (and wrap Blocks in Lazy) #18362
Conversation
let payload = lazyComponent._payload; | ||
let init = lazyComponent._init; | ||
try { | ||
return getComponentName(init(payload)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the component name can now have side-effects, causing data to load.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 8a6c32b:
|
Details of bundled changes.Comparing: 31a9e39...8a6c32b react
react-dom
react-art
react-test-renderer
react-native-renderer
react-reconciler
ReactDOM: size: -1.3%, gzip: -1.2% React: size: 🔺+2.7%, gzip: 🔺+2.4% Size changes (experimental) |
Details of bundled changes.Comparing: 31a9e39...8a6c32b react
react-dom
react-native-renderer
react-art
react-test-renderer
react-reconciler
ReactDOM: size: 0.0%, gzip: -0.0% React: size: 🔺+2.4%, gzip: 🔺+2.2% Size changes (stable) |
if (element.type.$$typeof === REACT_BLOCK_TYPE) { | ||
let type = element.type; | ||
if (type.$$typeof === REACT_LAZY_TYPE) { | ||
type = resolveLazyType(type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For updates, we attempt to initialize earlier to see if we can reconcile against the inner value. I only do this for Blocks right now to preserve backwards compatibility.
However, in non-legacy mode, we could reconcile against the resolved value of any type.
E.g. if I have two lazy components that resolve to the same thing, why shouldn't they reconcile against each other?
d26721f
to
5b2c1c9
Compare
4250bb1
to
aa99216
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
lazyComponent: LazyComponent<T, P>, | ||
): LazyComponent<T, P> | T { | ||
try { | ||
// If we can, let's peak at the resulting type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try { | ||
outerMemoType = init(payload); | ||
} catch (x) { | ||
outerMemoType = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just let it throw? If we got here, doesn't mean it's resolved anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. I think you’re right. I think that we’d only get here if the init is not deterministic. I.e. if it resuspends. That’s a bug in the lazy code but could possibly happen as a bug in a “user provided lazy”.
That would be a prod changing behavior so probably best to keep this since it wouldn’t happen in prod (although the call wouldn’t happen neither).
@@ -879,7 +879,13 @@ describe('ReactLazy', () => { | |||
}, | |||
); | |||
|
|||
expect(Scheduler).toFlushAndYield(['Started loading', 'Loading...']); | |||
if (__DEV__) { | |||
// Getting the name for the warning cause the loading to start early. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know how I feel about this. Can you motivate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we really should be refactoring the getComponentName stuff. One todo I have is to pass it a fiber because the type is not really enough. If we had the fiber we could read the .type off of that.
It’s called everywhere though so it’s some work to properly clean that up.
What annoys be a bit more is that we have that logic in prod. I suspect that’s what leads this PR to increase bundle size but just a little bit.
We probably should fork getComponentName and accompanying logic into specific DEV and prod versions for different use cases.
Regardless, I’m confident the fix is in the logging and not compromising the logic for lazy resolution.
Then resolve to a true Block inside.
initializeBlockComponentType(element.type); | ||
let type = element.type; | ||
if (type.$$typeof === REACT_LAZY_TYPE) { | ||
type = resolveLazyType(type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This got lost when we dropped Blocks. The benefit of Blocks was that we only needed this check if the tag was Block but now we need to do it for everything.
I wasn't happy with my approach in #18220
Exposing the status stuff leaks a lot of complexity downstream. It avoids taking on a virtual dispatch but also means that other code (like Flight and Blocks) can't take advantage of what Lazy already does.
Using the suspense protocol is actually really nice in terms of code size and complexity. The principle is simple:
The lazy component type can now be used to implement all kinds of lazy behavior. So I also use it to implement lazy Blocks.