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: ordering issue with Component re-render #4125

Merged
merged 3 commits into from
Sep 3, 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
2 changes: 1 addition & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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._dom;
return sibling._nextDom || sibling._dom;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function diffChildren(
oldVNode = oldChildren[i];
if (oldVNode && oldVNode.key == null && oldVNode._dom) {
if (oldVNode._dom == oldDom) {
oldVNode._parent = oldParentVNode;
Copy link
Member Author

Choose a reason for hiding this comment

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

I am currently trying to figure out why this part matters but it looks like oldVNode._parent refers to the new version of the parent-vnode here

oldDom = getDomSibling(oldVNode);
}

Expand Down
104 changes: 104 additions & 0 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,110 @@ describe('Fragment', () => {
expect(scratch.innerHTML).to.equal('<div>Hello</div>');
});

it('should preserve order for fragment switching', () => {
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>
);
}
}

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

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

it('should preserve order for nested fragment switching w/ child return', () => {
let set;
const Wrapper = ({ children }) => <Fragment>{children}</Fragment>;
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>
);
}
}

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

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

it('should preserve order for nested fragment switching', () => {
let set;
const Wrapper = () => (
<Fragment>
<Foo />
</Fragment>
);
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>
);
}
}

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

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

it('should preserve state with reordering in multiple levels', () => {
function Foo({ condition }) {
return condition ? (
Expand Down
Loading