Skip to content

Commit 8931a49

Browse files
committed
fix: add React 17 support for lazy, related to #1425
1 parent d5c8aa2 commit 8931a49

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/reconciler/fiberUpdater.js

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-underscore-dangle */
12
import React from 'react';
23
import configuration from '../configuration';
34
import { enterHotUpdate } from '../global/generation';
@@ -6,10 +7,36 @@ import { resolveType } from './resolver';
67

78
const lazyConstructor = '_ctor';
89

10+
const getLazyConstructor = target => {
11+
// React 16
12+
if (target[lazyConstructor]) {
13+
return target[lazyConstructor];
14+
}
15+
16+
// React 17
17+
if (target._payload) {
18+
return target._payload._result;
19+
}
20+
return null;
21+
};
22+
23+
const setLazyConstructor = (target, replacement) => {
24+
replacement.isPatchedByReactHotLoader = true;
25+
26+
// React 16
27+
if (target[lazyConstructor]) {
28+
target[lazyConstructor] = replacement;
29+
}
30+
// React 17
31+
if (target._payload) {
32+
target._payload._result = replacement;
33+
}
34+
};
35+
936
const patchLazyConstructor = target => {
10-
if (!configuration.trackTailUpdates && !target[lazyConstructor].isPatchedByReactHotLoader) {
11-
const ctor = target[lazyConstructor];
12-
target[lazyConstructor] = () =>
37+
if (!configuration.trackTailUpdates && !getLazyConstructor(target).isPatchedByReactHotLoader) {
38+
const ctor = getLazyConstructor(target);
39+
setLazyConstructor(target, () =>
1340
ctor().then(m => {
1441
const C = resolveType(m.default);
1542
// chunks has been updated - new hot loader process is taking a place
@@ -30,14 +57,14 @@ const patchLazyConstructor = target => {
3057
</AppContainer>
3158
)),
3259
};
33-
});
34-
target[lazyConstructor].isPatchedByReactHotLoader = true;
60+
}),
61+
);
3562
}
3663
};
3764

3865
export const updateLazy = (target, type) => {
39-
const ctor = type[lazyConstructor];
40-
if (target[lazyConstructor] !== type[lazyConstructor]) {
66+
const ctor = getLazyConstructor(type);
67+
if (getLazyConstructor(target) !== ctor) {
4168
// just execute `import` and RHL.register will do the job
4269
ctor();
4370
}

0 commit comments

Comments
 (0)