Skip to content

Commit

Permalink
fix: add React 17 support for lazy, related to #1425
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 22, 2020
1 parent d5c8aa2 commit 8931a49
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/reconciler/fiberUpdater.js
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import React from 'react';
import configuration from '../configuration';
import { enterHotUpdate } from '../global/generation';
Expand All @@ -6,10 +7,36 @@ import { resolveType } from './resolver';

const lazyConstructor = '_ctor';

const getLazyConstructor = target => {
// React 16
if (target[lazyConstructor]) {
return target[lazyConstructor];
}

// React 17
if (target._payload) {
return target._payload._result;
}
return null;
};

const setLazyConstructor = (target, replacement) => {
replacement.isPatchedByReactHotLoader = true;

// React 16
if (target[lazyConstructor]) {
target[lazyConstructor] = replacement;
}
// React 17
if (target._payload) {
target._payload._result = replacement;
}
};

const patchLazyConstructor = target => {
if (!configuration.trackTailUpdates && !target[lazyConstructor].isPatchedByReactHotLoader) {
const ctor = target[lazyConstructor];
target[lazyConstructor] = () =>
if (!configuration.trackTailUpdates && !getLazyConstructor(target).isPatchedByReactHotLoader) {
const ctor = getLazyConstructor(target);
setLazyConstructor(target, () =>
ctor().then(m => {
const C = resolveType(m.default);
// chunks has been updated - new hot loader process is taking a place
Expand All @@ -30,14 +57,14 @@ const patchLazyConstructor = target => {
</AppContainer>
)),
};
});
target[lazyConstructor].isPatchedByReactHotLoader = true;
}),
);
}
};

export const updateLazy = (target, type) => {
const ctor = type[lazyConstructor];
if (target[lazyConstructor] !== type[lazyConstructor]) {
const ctor = getLazyConstructor(type);
if (getLazyConstructor(target) !== ctor) {
// just execute `import` and RHL.register will do the job
ctor();
}
Expand Down

0 comments on commit 8931a49

Please sign in to comment.