-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Support sync thenables for lazy() #14626
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,10 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T { | |
} | ||
}, | ||
); | ||
lazyComponent._result = thenable; | ||
// Don't race with a sync thenable | ||
if (lazyComponent._status === Pending) { | ||
lazyComponent._result = thenable; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also do the same for failure. But doesn't seem worth the extra code to me. Could be a recursive call but then there's more risk of messing it up and going into a stack overflow. Meh. |
||
throw thenable; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -61,6 +61,23 @@ describe('ReactLazy', () => { | |||||
expect(root).toMatchRenderedOutput('Hi again'); | ||||||
}); | ||||||
|
||||||
it('can resolve synchronously without suspending', async () => { | ||||||
const LazyText = lazy(() => ({ | ||||||
then(cb) { | ||||||
cb({default: Text}); | ||||||
}, | ||||||
})); | ||||||
|
||||||
const root = ReactTestRenderer.create( | ||||||
<Suspense fallback={<Text text="Loading..." />}> | ||||||
<LazyText text="Hi" /> | ||||||
</Suspense>, | ||||||
); | ||||||
|
||||||
expect(ReactTestRenderer).toHaveYielded(['Loading...', 'Hi']); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
expect(root).toMatchRenderedOutput('Hi'); | ||||||
}); | ||||||
|
||||||
it('multiple lazy components', async () => { | ||||||
function Foo() { | ||||||
return <Text text="Foo" />; | ||||||
|
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.
Alt suggestion:
Since if it's already resolved we don't need to throw the thenable
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.
Aaaah. That's why it had the loading state. Thanks.