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 child reordering with memo #2896

Merged
merged 1 commit into from
Jan 1, 2021
Merged
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
65 changes: 62 additions & 3 deletions compat/test/browser/memo.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { setupRerender } from 'preact/test-utils';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import React, { createElement, Component, render, memo } from 'preact/compat';
import {
createEvent,
setupScratch,
teardown
} from '../../../test/_util/helpers';
import React, {
createElement,
Component,
render,
memo,
useState
} from 'preact/compat';
import { li, ol } from '../../../test/_util/dom';

const h = React.createElement;

describe('memo()', () => {
let scratch, rerender;
/** @type {HTMLDivElement} */
let scratch;

/** @type {() => void} */
let rerender;

beforeEach(() => {
scratch = setupScratch();
Expand Down Expand Up @@ -172,4 +187,48 @@ describe('memo()', () => {
expect(ref.current).not.to.be.undefined;
expect(ref.current).to.be.instanceOf(Foo);
});

it('should not unnecessarily reorder children #2895', () => {
const array = [{ name: 'A' }, { name: 'B' }, { name: 'C' }, { name: 'D' }];

const List = () => {
const [selected, setSelected] = useState('');
return (
<ol>
{array.map(item => (
<ListItem
{...{
isSelected: item.name === selected,
setSelected,
...item
}}
key={item.name}
/>
))}
</ol>
);
};

const ListItem = memo(({ name, isSelected, setSelected }) => {
const handleClick = () => setSelected(name);
return (
<li class={isSelected ? 'selected' : null} onClick={handleClick}>
{name}
</li>
);
});

render(<List />, scratch);
expect(scratch.innerHTML).to.equal(
`<ol><li>A</li><li>B</li><li>C</li><li>D</li></ol>`
);

let listItem = scratch.querySelector('li:nth-child(3)');
listItem.dispatchEvent(createEvent('click'));
rerender();

expect(scratch.innerHTML).to.equal(
`<ol><li>A</li><li>B</li><li class="selected">C</li><li>D</li></ol>`
);
});
});
6 changes: 5 additions & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,14 @@ function reorderChildren(childVNode, oldDom, parentDom) {
for (let tmp = 0; tmp < childVNode._children.length; tmp++) {
let vnode = childVNode._children[tmp];
if (vnode) {
// We typically enter this code path on sCU bailout, where we copy
// oldVNode._children to newVNode._children. If that is the case, we need
// to update the old children's _parent pointer to point to the newVNode
// (childVNode here).
vnode._parent = childVNode;

if (typeof vnode.type == 'function') {
reorderChildren(vnode, oldDom, parentDom);
oldDom = reorderChildren(vnode, oldDom, parentDom);
} else {
oldDom = placeChild(
parentDom,
Expand Down
2 changes: 1 addition & 1 deletion test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ describe('Fragment', () => {
expect(scratch.innerHTML).to.equal('<div>Hello</div>');
});

it.skip('should preserve state between double nested fragment and double nested array', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This test was skipped in the world where we flattened arrays. Since we now convert arrays to Fragments it passes. Went ahead and enabled it.

it('should preserve state between double nested fragment and double nested array', () => {
function Foo({ condition }) {
return condition ? (
<Fragment>
Expand Down