Skip to content

Commit 36db538

Browse files
author
Andrew Clark
authored
Bugfix for #13886 (#13896)
Fixes a bug where a lazy component does not cache the result of its constructor.
1 parent 6938dca commit 36db538

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

packages/react-reconciler/src/ReactFiberLazyComponent.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@
77
* @flow
88
*/
99

10-
import type {LazyComponent} from 'shared/ReactLazyComponent';
10+
import type {LazyComponent, Thenable} from 'shared/ReactLazyComponent';
1111

1212
import {Resolved, Rejected, Pending} from 'shared/ReactLazyComponent';
1313
import warning from 'shared/warning';
1414

1515
export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
1616
const status = lazyComponent._status;
17+
const result = lazyComponent._result;
1718
switch (status) {
18-
case Resolved:
19-
const Component: T = lazyComponent._result;
19+
case Resolved: {
20+
const Component: T = result;
2021
return Component;
21-
case Rejected:
22-
throw lazyComponent._result;
23-
case Pending:
24-
throw lazyComponent;
22+
}
23+
case Rejected: {
24+
const error: mixed = result;
25+
throw error;
26+
}
27+
case Pending: {
28+
const thenable: Thenable<T, mixed> = result;
29+
throw thenable;
30+
}
2531
default: {
2632
lazyComponent._status = Pending;
2733
const ctor = lazyComponent._ctor;
@@ -52,6 +58,7 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
5258
}
5359
},
5460
);
61+
lazyComponent._result = thenable;
5562
throw thenable;
5663
}
5764
}

packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js

+52-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe('ReactLazy', () => {
2121
return props.text;
2222
}
2323

24+
function delay(ms) {
25+
return new Promise(resolve => setTimeout(() => resolve(), ms));
26+
}
27+
2428
async function fakeImport(result) {
2529
return {default: result};
2630
}
@@ -40,7 +44,7 @@ describe('ReactLazy', () => {
4044
expect(root).toFlushAndYield(['Loading...']);
4145
expect(root).toMatchRenderedOutput(null);
4246

43-
await LazyText;
47+
await Promise.resolve();
4448

4549
expect(root).toFlushAndYield(['Hi']);
4650
expect(root).toMatchRenderedOutput('Hi');
@@ -55,6 +59,47 @@ describe('ReactLazy', () => {
5559
expect(root).toMatchRenderedOutput('Hi again');
5660
});
5761

62+
it('multiple lazy components', async () => {
63+
function Foo() {
64+
return <Text text="Foo" />;
65+
}
66+
67+
function Bar() {
68+
return <Text text="Bar" />;
69+
}
70+
71+
const promiseForFoo = delay(1000).then(() => fakeImport(Foo));
72+
const promiseForBar = delay(2000).then(() => fakeImport(Bar));
73+
74+
const LazyFoo = lazy(() => promiseForFoo);
75+
const LazyBar = lazy(() => promiseForBar);
76+
77+
const root = ReactTestRenderer.create(
78+
<Suspense fallback={<Text text="Loading..." />}>
79+
<LazyFoo />
80+
<LazyBar />
81+
</Suspense>,
82+
{
83+
unstable_isConcurrent: true,
84+
},
85+
);
86+
87+
expect(root).toFlushAndYield(['Loading...']);
88+
expect(root).toMatchRenderedOutput(null);
89+
90+
jest.advanceTimersByTime(1000);
91+
await promiseForFoo;
92+
93+
expect(root).toFlushAndYield(['Foo', 'Loading...']);
94+
expect(root).toMatchRenderedOutput(null);
95+
96+
jest.advanceTimersByTime(1000);
97+
await promiseForBar;
98+
99+
expect(root).toFlushAndYield(['Foo', 'Bar']);
100+
expect(root).toMatchRenderedOutput('FooBar');
101+
});
102+
58103
it('does not support arbitrary promises, only module objects', async () => {
59104
spyOnDev(console, 'error');
60105

@@ -71,7 +116,7 @@ describe('ReactLazy', () => {
71116
expect(root).toFlushAndYield(['Loading...']);
72117
expect(root).toMatchRenderedOutput(null);
73118

74-
await LazyText;
119+
await Promise.resolve();
75120

76121
if (__DEV__) {
77122
expect(console.error).toHaveBeenCalledTimes(1);
@@ -100,7 +145,7 @@ describe('ReactLazy', () => {
100145
expect(root).toMatchRenderedOutput(null);
101146

102147
try {
103-
await LazyText;
148+
await Promise.resolve();
104149
} catch (e) {}
105150

106151
expect(root).toFlushAndThrow('Bad network');
@@ -176,7 +221,7 @@ describe('ReactLazy', () => {
176221
expect(root).toFlushAndYield(['Loading...']);
177222
expect(root).toMatchRenderedOutput(null);
178223

179-
await LazyText;
224+
await Promise.resolve();
180225

181226
expect(root).toFlushAndYield(['Hi']);
182227
expect(root).toMatchRenderedOutput('Hi');
@@ -226,7 +271,7 @@ describe('ReactLazy', () => {
226271
expect(root).toFlushAndYield(['Loading...']);
227272
expect(root).toMatchRenderedOutput(null);
228273

229-
await Lazy;
274+
await Promise.resolve();
230275

231276
expect(root).toFlushAndYield(['Lazy', 'Sibling', 'A']);
232277
expect(root).toMatchRenderedOutput('SiblingA');
@@ -256,7 +301,7 @@ describe('ReactLazy', () => {
256301
expect(root).toFlushAndYield(['Started loading', 'Loading...']);
257302
expect(root).toMatchRenderedOutput(null);
258303

259-
await LazyFoo;
304+
await Promise.resolve();
260305

261306
expect(() => {
262307
expect(root).toFlushAndYield(['A', 'B']);
@@ -303,8 +348,7 @@ describe('ReactLazy', () => {
303348
expect(root).toMatchRenderedOutput(null);
304349
expect(ref.current).toBe(null);
305350

306-
await LazyClass;
307-
await LazyForwardRef;
351+
await Promise.resolve();
308352

309353
expect(root).toFlushAndYield(['Foo', 'forwardRef', 'Bar']);
310354
expect(root).toMatchRenderedOutput('FooBar');

0 commit comments

Comments
 (0)