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

Eagerly unmount placeholders #4090

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export function diffChildren(
// Terser removes the `continue` here and wraps the loop body
// in a `if (childVNode) { ... } condition
if (childVNode == null) {
oldVNode = oldChildren[i];
if (oldVNode && oldVNode.key == null && oldVNode._dom) {
if (oldVNode._dom == oldDom) {
oldDom = oldDom.nextSibling;
}

unmount(oldVNode, oldVNode, false);
}

andrewiggins marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand Down
26 changes: 12 additions & 14 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,14 +1328,14 @@ describe('Fragment', () => {
'rendering from true to false'
);
expectDomLogToBe([
// Remove 1 & 2 (replaced with null)
'<li>0.remove()',
'<li>1.remove()',
// Mount 3 & 4
'<li>.appendChild(#text)',
'<ol>0122.appendChild(<li>3)',
'<ol>22.appendChild(<li>3)',
'<li>.appendChild(#text)',
'<ol>01223.appendChild(<li>4)',
// Remove 1 & 2 (replaced with null)
'<li>0.remove()',
'<li>1.remove()'
'<ol>223.appendChild(<li>4)'
]);

clearLog();
Expand Down Expand Up @@ -1399,14 +1399,14 @@ describe('Fragment', () => {
'rendering from true to false'
);
expectDomLogToBe([
// Remove 1 & 2 (replaced with null)
'<li>0.remove()',
'<li>1.remove()',
// Mount 4 & 5
'<li>.appendChild(#text)',
'<ol>0123.appendChild(<li>4)',
'<ol>23.appendChild(<li>4)',
'<li>.appendChild(#text)',
'<ol>01234.appendChild(<li>5)',
// Remove 1 & 2 (replaced with null)
'<li>0.remove()',
'<li>1.remove()'
'<ol>234.appendChild(<li>5)'
]);

clearLog();
Expand Down Expand Up @@ -2609,11 +2609,9 @@ describe('Fragment', () => {
rerender();
expect(scratch.innerHTML).to.equal(bottom);
expectDomLogToBe([
'<div>top panelNavigationContent.insertBefore(<div>Navigation, <div>top panel)',
'<div>Navigationtop panelContent.insertBefore(<div>Content, <div>top panel)',
'<div>top panel.remove()',
'<div>.appendChild(#text)',
'<div>NavigationContenttop panel.insertBefore(<div>bottom panel, <div>top panel)',
'<div>top panel.remove()'
'<div>NavigationContent.appendChild(<div>bottom panel)'
]);

clearLog();
Expand Down
52 changes: 52 additions & 0 deletions test/browser/placeholders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,56 @@ describe('null placeholders', () => {
'#text.remove()'
]);
});

it('when set through the children prop', () => {
/** @type {(state: { value: boolean }) => void} */
let setState;
const Iframe = () => {
// Using a div here to make debugging tests in devtools a little less
// noisy. The dom ops still assert that the iframe isn't moved.
//
// return <iframe src="https://codesandbox.io/s/runtime-silence-no4zx" />;
return <div>Iframe</div>;
};

const Test2 = () => <div>Test2</div>;
const Test3 = () => <div>Test3</div>;

class App extends Component {
constructor(props) {
super(props);
this.state = { value: true };
setState = this.setState.bind(this);
}

render(props, state) {
return (
<div>
<Test2 />
{state.value && <Test3 />}
{props.children}
</div>
);
}
}

render(
<App>
<Iframe />
</App>,
scratch
);

expect(scratch.innerHTML).to.equal(
'<div><div>Test2</div><div>Test3</div><div>Iframe</div></div>'
);
clearLog();
setState({ value: false });
rerender();

expect(scratch.innerHTML).to.equal(
'<div><div>Test2</div><div>Iframe</div></div>'
);
expect(getLog()).to.deep.equal(['<div>Test3.remove()']);
});
});
4 changes: 2 additions & 2 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,8 +1101,8 @@ describe('render()', () => {
'<div><iframe src="https://codesandbox.io/s/runtime-silence-no4zx"></iframe></div>'
);
expect(getLog()).to.deep.equal([
'<div>Test2.remove()',
'<div>Test3.remove()'
'<div>Test3.remove()',
'<div>Test2.remove()'
]);
});

Expand Down