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

Manually track children's index & fix parent pointers when rerendering components #4170

Merged
merged 3 commits into from
Oct 26, 2023
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
1 change: 1 addition & 0 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
_hydrating: null,
constructor: undefined,
_original: --vnodeId,
_index: -1,
__source,
__self
};
Expand Down
1 change: 1 addition & 0 deletions mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"$_dom": "__e",
"$_hydrating": "__h",
"$_component": "__c",
"$_index": "__i",
"$__html": "__html",
"$_parent": "__",
"$_pendingError": "__E",
Expand Down
10 changes: 8 additions & 2 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function getDomSibling(vnode, childIndex) {
if (childIndex == null) {
// Use childIndex==null as a signal to resume the search from the vnode's sibling
return vnode._parent
? getDomSibling(vnode._parent, vnode._parent._children.indexOf(vnode) + 1)
? getDomSibling(vnode._parent, vnode._index + 1)
: null;
}

Expand All @@ -103,7 +103,7 @@ export function getDomSibling(vnode, childIndex) {
// Since updateParentDomPointers keeps _dom pointer correct,
// we can rely on _dom to tell us if this subtree contains a
// rendered DOM node, and what the first rendered DOM node is
return sibling._nextDom || sibling._dom;
return sibling._dom;
}
}

Expand All @@ -130,6 +130,12 @@ function renderComponent(component) {
const oldVNode = assign({}, vnode);
oldVNode._original = vnode._original + 1;

if (oldVNode._children) {
oldVNode._children.forEach(child => {
if (child) child._parent = oldVNode;
});
}

diff(
parentDom,
vnode,
Expand Down
3 changes: 2 additions & 1 deletion src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export function createVNode(type, props, key, ref, original) {
_component: null,
_hydrating: null,
constructor: undefined,
_original: original == null ? ++vnodeId : original
_original: original == null ? ++vnodeId : original,
_index: -1
};

// Only invoke the vnode hook if this was *not* a direct copy:
Expand Down
4 changes: 3 additions & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function diffChildren(
) {
let i,
j,
/** @type {import('../internal').VNode} */
oldVNode,
/** @type {import('../internal').VNode} */
childVNode,
newDom,
firstChildDom,
Expand Down Expand Up @@ -111,7 +113,6 @@ export function diffChildren(
oldVNode = oldChildren[i];
if (oldVNode && oldVNode.key == null && oldVNode._dom) {
if (oldVNode._dom == oldDom) {
oldVNode._parent = oldParentVNode;
oldDom = getDomSibling(oldVNode);
}

Expand All @@ -124,6 +125,7 @@ export function diffChildren(

childVNode._parent = newParentVNode;
childVNode._depth = newParentVNode._depth + 1;
childVNode._index = i;

let skewedIndex = i + skew;
const matchingIndex = findMatchingIndex(
Expand Down
1 change: 1 addition & 0 deletions src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface VNode<P = {}> extends preact.VNode<P> {
_hydrating: boolean | null;
constructor: undefined;
_original: number;
_index: number;
}

export interface Component<P = {}, S = {}> extends preact.Component<P, S> {
Expand Down
78 changes: 78 additions & 0 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,84 @@ describe('Fragment', () => {
);
});

it('should preserve order for fragment switching with sibling DOM', () => {
/** @type {() => void} */
let set;
class Foo extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, data: null };
set = this.setState.bind(this);
}
render(props, { isLoading, data }) {
return (
<Fragment>
<div>HEADER</div>
{isLoading ? <div>Loading...</div> : null}
{data ? <div>Content: {data}</div> : null}
<div>FOOTER</div>
</Fragment>
);
}
}

render(<Foo />, scratch);
expect(scratch.innerHTML).to.equal(
'<div>HEADER</div><div>Loading...</div><div>FOOTER</div>'
);

set({ isLoading: false, data: 2 });
rerender();
expect(scratch.innerHTML).to.equal(
'<div>HEADER</div><div>Content: 2</div><div>FOOTER</div>'
);
});

it('should preserve order for fragment switching with sibling Components', () => {
/** @type {() => void} */
let set;
class Foo extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, data: null };
set = this.setState.bind(this);
}
render(props, { isLoading, data }) {
return (
<Fragment>
<div>HEADER</div>
{isLoading ? <div>Loading...</div> : null}
{data ? <div>Content: {data}</div> : null}
</Fragment>
);
}
}

function Footer() {
return <div>FOOTER</div>;
}

function App() {
return (
<Fragment>
<Foo />
<Footer />
</Fragment>
);
}

render(<App />, scratch);
expect(scratch.innerHTML).to.equal(
'<div>HEADER</div><div>Loading...</div><div>FOOTER</div>'
);

set({ isLoading: false, data: 2 });
rerender();
expect(scratch.innerHTML).to.equal(
'<div>HEADER</div><div>Content: 2</div><div>FOOTER</div>'
);
});

it('should preserve order for nested fragment switching w/ child return', () => {
let set;
const Wrapper = ({ children }) => <Fragment>{children}</Fragment>;
Expand Down