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 case where we would omit the ref from reused vnodes #3696

Merged
merged 4 commits into from Aug 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/diff/children.js
Expand Up @@ -83,7 +83,7 @@ export function diffChildren(
childVNode.type,
childVNode.props,
childVNode.key,
null,
childVNode.ref ? childVNode.ref : null,
childVNode._original
);
} else {
Expand Down Expand Up @@ -244,14 +244,7 @@ function reorderChildren(childVNode, oldDom, parentDom) {
if (typeof vnode.type == 'function') {
oldDom = reorderChildren(vnode, oldDom, parentDom);
} else {
oldDom = placeChild(
parentDom,
vnode,
vnode,
c,
vnode._dom,
oldDom
);
oldDom = placeChild(parentDom, vnode, vnode, c, vnode._dom, oldDom);
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/browser/refs.test.js
Expand Up @@ -478,4 +478,68 @@ describe('refs', () => {
render(<App />, scratch);
expect(el).to.not.be.equal(null);
});

it('should not remove refs for memoized components keyed', () => {
const ref = createRef();
const element = <div ref={ref}>hey</div>;
function App(props) {
return <div key={props.count}>{element}</div>;
}

render(<App count={0} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
});

it('should not remove refs for memoized components unkeyed', () => {
const ref = createRef();
const element = <div ref={ref}>hey</div>;
function App(props) {
return <div>{element}</div>;
}

render(<App count={0} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(ref.current).to.equal(scratch.firstChild.firstChild);
});

it('should properly call null for memoized components keyed', () => {
const calls = [];
const element = <div ref={x => calls.push(x)}>hey</div>;
function App(props) {
return <div key={props.count}>{element}</div>;
}

render(<App count={0} />, scratch);
expect(calls[0]).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(calls[1]).to.equal(null);
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 seems to still be called in the wrong order for functions i.e. applyRef(null); applyRef(element);

This could be similar to v11 ref ordering #3493 i.e. we basically unmount the parent but have already applied the ref as part of the diff in the child - this could be because our equality logic falls apart as we have no oldVNode to match against

expect(calls[2]).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(calls[3]).to.equal(null);
expect(calls[4]).to.equal(scratch.firstChild.firstChild);
});

it('should properly call null for memoized components unkeyed', () => {
const calls = [];
const element = <div ref={x => calls.push(x)}>hey</div>;
function App(props) {
return <div>{element}</div>;
}

render(<App count={0} />, scratch);
expect(calls[0]).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(calls[1]).to.equal(null);
expect(calls[2]).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(calls[3]).to.equal(null);
expect(calls[4]).to.equal(scratch.firstChild.firstChild);
});
});