Skip to content
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

fix: Memoed Component bails while detached #4711

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compat/src/suspense.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function detachedClone(vnode, detachedParent, parentDom) {
if (vnode._component._parentDom === parentDom) {
vnode._component._parentDom = detachedParent;
}

vnode._component._force = true;

vnode._component = null;
}

Expand Down
62 changes: 58 additions & 4 deletions compat/test/browser/suspense.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupRerender } from 'preact/test-utils';
import { act, setupRerender } from 'preact/test-utils';
import React, {
createElement,
render,
Expand All @@ -9,7 +9,8 @@ import React, {
createContext,
useState,
useEffect,
useLayoutEffect
useLayoutEffect,
memo
} from 'preact/compat';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { createLazy, createSuspender } from './suspense-utils';
Expand Down Expand Up @@ -1501,8 +1502,8 @@ describe('suspense', () => {

/** @type {() => void} */
let hide,
/** @type {(v) => void} */
setValue;
/** @type {(v) => void} */
setValue;

class Conditional extends Component {
constructor(props) {
Expand Down Expand Up @@ -2159,4 +2160,57 @@ describe('suspense', () => {
expect(scratch.innerHTML).to.equal('<div><p>hello world</p></div>');
});
});

it('should re-execute descendant memoed component effect when lazy boundary resolves', async () => {
const MemodComp = memo(() => {
const [state, setState] = useState('Memod effect not executed');
useEffect(() => {
setState('Memod effect executed');
}, []);
return <span>{state}</span>;
});

const NormalComp = () => {
const [state, setState] = useState('effect not executed');
useEffect(() => {
setState('effect executed');
}, []);
return <span>{state}</span>;
};

/** @type {() => Promise<void>} */
let resolve;
const LazyComp = lazy(() => {
const p = new Promise(res => {
resolve = () => {
res({ default: NormalComp });
return p;
};
});

return p;
});

render(
<Suspense fallback={<div>Suspended...</div>}>
<div>
<MemodComp />
<LazyComp />
</div>
</Suspense>,
scratch
);

rerender();

expect(scratch.innerHTML).to.eql(`<div>Suspended...</div>`);

await resolve();

await act(() => rerender());

return expect(scratch.innerHTML).to.eql(
`<div><span>Memod effect executed</span><span>effect executed</span></div>`
);
});
});
Loading