Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 14 additions & 6 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ export function diffChildren(
childVNode._flags & INSERT_VNODE ||
oldVNode._children === childVNode._children
) {
oldDom = insert(childVNode, oldDom, parentDom);
oldDom = insert(
childVNode,
oldDom,
parentDom,
!(childVNode._flags & INSERT_VNODE) /* shouldSkipDomUpdate */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably hoist this check into a variable so we don't do it twice but don't feel strongly about that as it's just a bit check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoviDeCroock Thanks for your feedback! Pushed a commit that hoists the check into a variable.

After sleeping on it, I decided to also get rid of double negation to make it simpler to read, so I renamed shouldSkipDomUpdate to shouldPlace. Please let me know if you have any preferences on the flag name or anything else.

Copy link
Contributor Author

@vasylenkoval vasylenkoval Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-requested approvals, will cherry-pick in another PR to 10.x right after review.

);
} else if (typeof childVNode.type == 'function' && result !== UNDEFINED) {
oldDom = result;
} else if (newDom) {
Expand Down Expand Up @@ -342,9 +347,10 @@ function constructNewChildrenArray(
* @param {VNode} parentVNode
* @param {PreactElement} oldDom
* @param {PreactElement} parentDom
* @param {boolean} shouldSkipDomUpdate
* @returns {PreactElement}
*/
function insert(parentVNode, oldDom, parentDom) {
function insert(parentVNode, oldDom, parentDom, shouldSkipDomUpdate) {
// Note: VNodes in nested suspended trees may be missing _children.
if (typeof parentVNode.type == 'function') {
let children = parentVNode._children;
Expand All @@ -355,16 +361,18 @@ function insert(parentVNode, oldDom, parentDom) {
// children's _parent pointer to point to the newVNode (parentVNode
// here).
children[i]._parent = parentVNode;
oldDom = insert(children[i], oldDom, parentDom);
oldDom = insert(children[i], oldDom, parentDom, shouldSkipDomUpdate);
}
}

return oldDom;
} else if (parentVNode._dom != oldDom) {
if (oldDom && parentVNode.type && !oldDom.parentNode) {
oldDom = getDomSibling(parentVNode);
if (!shouldSkipDomUpdate) {
if (oldDom && parentVNode.type && !oldDom.parentNode) {
oldDom = getDomSibling(parentVNode);
}
parentDom.insertBefore(parentVNode._dom, oldDom || NULL);
}
parentDom.insertBefore(parentVNode._dom, oldDom || NULL);
oldDom = parentVNode._dom;
}

Expand Down
59 changes: 58 additions & 1 deletion test/browser/lifecycles/shouldComponentUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setupRerender } from 'preact/test-utils';
import { createElement, render, Component, Fragment } from 'preact';
import { vi } from 'vitest';
import { setupScratch, teardown } from '../../_util/helpers';
import { logCall, clearLog } from '../../_util/logCall';
import { logCall, getLog, clearLog } from '../../_util/logCall';

/** @jsx createElement */

Expand Down Expand Up @@ -962,4 +962,61 @@ describe('Lifecycle methods', () => {
`<div>Before</div><div>Component</div><div>After</div>`
);
});

it('should not re-insert memoized items that keep their relative order after swap', () => {
class MemoizedItem extends Component {
shouldComponentUpdate(nextProps) {
return nextProps.value !== this.props.value;
}
render() {
return <div>{this.props.value}</div>;
}
}

const App = ({ items }) => (
<div>
{items.map(value => (
<MemoizedItem key={value} value={value} />
))}
</div>
);

render(<App items={[1, 2, 3, 4, 5, 6, 7]} />, scratch);

function renderItemsAndAssert({ items, expectedLog }) {
clearLog();
render(<App items={items} />, scratch);
expect(scratch.innerHTML).to.equal(
`<div>${items.map(value => `<div>${value}</div>`).join('')}</div>`
);
expect(getLog()).to.deep.equal(expectedLog);
}

// Swap 1 and 7
renderItemsAndAssert({
items: [7, 2, 3, 4, 5, 6, 1],
expectedLog: [
'<div>1234567.insertBefore(<div>7, <div>1)',
'<div>7123456.appendChild(<div>1)'
]
});

// Swap 2 and 6
renderItemsAndAssert({
items: [7, 6, 3, 4, 5, 2, 1],
expectedLog: [
'<div>7234561.insertBefore(<div>6, <div>2)',
'<div>7623451.insertBefore(<div>2, <div>1)'
]
});

// Swap 3 and 5
renderItemsAndAssert({
items: [7, 6, 5, 4, 3, 2, 1],
expectedLog: [
'<div>7634521.insertBefore(<div>5, <div>3)',
'<div>7653421.insertBefore(<div>3, <div>2)'
]
});
});
});
Loading