Skip to content

Commit eab1036

Browse files
author
willxywang
committed
fix: Memoed Component bails while detached
1 parent 1effaa6 commit eab1036

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

compat/src/suspense.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ function detachedClone(vnode, detachedParent, parentDom) {
5757
if (vnode._component._parentDom === parentDom) {
5858
vnode._component._parentDom = detachedParent;
5959
}
60+
61+
vnode._component._force = true;
62+
6063
vnode._component = null;
6164
}
6265

compat/test/browser/suspense.test.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import React, {
99
createContext,
1010
useState,
1111
useEffect,
12-
useLayoutEffect
12+
useLayoutEffect,
13+
memo
1314
} from 'preact/compat';
1415
import { setupScratch, teardown } from '../../../test/_util/helpers';
1516
import { createLazy, createSuspender } from './suspense-utils';
@@ -1501,8 +1502,8 @@ describe('suspense', () => {
15011502

15021503
/** @type {() => void} */
15031504
let hide,
1504-
/** @type {(v) => void} */
1505-
setValue;
1505+
/** @type {(v) => void} */
1506+
setValue;
15061507

15071508
class Conditional extends Component {
15081509
constructor(props) {
@@ -2159,4 +2160,38 @@ describe('suspense', () => {
21592160
expect(scratch.innerHTML).to.equal('<div><p>hello world</p></div>');
21602161
});
21612162
});
2163+
2164+
it('should correctly render memod child component', () => {
2165+
const Memod = memo(() => <span>Memod Component has rendered</span>);
2166+
2167+
/** @type {() => Promise<void>} */
2168+
let resolve;
2169+
const Lazy = lazy(() => {
2170+
const p = new Promise(res => {
2171+
resolve = () => {
2172+
res({ default: Memod });
2173+
return p;
2174+
};
2175+
});
2176+
2177+
return p;
2178+
});
2179+
2180+
render(
2181+
<Suspense fallback={<div>Suspended...</div>}>
2182+
<Lazy />
2183+
</Suspense>,
2184+
scratch
2185+
);
2186+
rerender();
2187+
2188+
expect(scratch.innerHTML).to.eql(`<div>Suspended...</div>`);
2189+
2190+
return resolve().then(() => {
2191+
rerender();
2192+
expect(scratch.innerHTML).to.eql(
2193+
`<span>Memod Component has rendered</span>`
2194+
);
2195+
});
2196+
});
21622197
});

0 commit comments

Comments
 (0)