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 duplicate ids when Fragments are involved #3758

Merged
merged 3 commits into from
Oct 6, 2022
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
5 changes: 3 additions & 2 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, options } from 'preact';
import { options } from 'preact';

/** @type {number} */
let currentIndex;
Expand Down Expand Up @@ -30,7 +30,8 @@ options._diff = vnode => {
if (
typeof vnode.type === 'function' &&
!vnode._mask &&
vnode.type !== Fragment
// Ignore root Fragment node
vnode._parent !== null
) {
vnode._mask =
(vnode._parent && vnode._parent._mask ? vnode._parent._mask : '') +
Expand Down
80 changes: 79 additions & 1 deletion hooks/test/browser/useId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,84 @@ describe('useId', () => {

scratch.innerHTML = rtsOutput;
hydrate(<Component />, scratch);
expect(rtsOutput === scratch.innerHTML).to.equal(true);
expect(rtsOutput).to.equal(scratch.innerHTML);
});

it('should be unique across Fragments', () => {
const ids = [];
function Foo() {
const id = useId();
ids.push(id);
return <p>{id}</p>;
}

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

render(<App />, scratch);

expect(ids[0]).not.to.equal(ids[1]);
});

it('should match implicite Fragments with RTS', () => {
function Foo() {
const id = useId();
return <p>{id}</p>;
}

function Bar(props) {
return props.children;
}

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

const rtsOutput = rts(<App />);

scratch.innerHTML = rtsOutput;
hydrate(<App />, scratch);
expect(rtsOutput).to.equal(scratch.innerHTML);
});

it('should skip component top level Fragment child', () => {
const Wrapper = ({ children }) => {
return <Fragment>{children}</Fragment>;
};

function Foo() {
const id = useId();
return <p>{id}</p>;
}

function App() {
const id = useId();
return (
<div>
<p>{id}</p>
<Wrapper>
<Foo />
</Wrapper>
</div>
);
}

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div><p>P481</p><p>P476951</p></div>');
});
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
"mocha": "^8.2.1",
"npm-merge-driver-install": "^1.1.1",
"npm-run-all": "^4.0.0",
"preact-render-to-string": "^5.2.4",
"preact-render-to-string": "^5.2.5",
"prettier": "^1.18.2",
"prop-types": "^15.7.2",
"sade": "^1.7.4",
Expand Down