Skip to content

Commit

Permalink
retain masks across useId invocations (#4245)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Jan 2, 2024
1 parent 13b0afb commit e1f0d3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let oldBeforeRender = options._render;
let oldAfterDiff = options.diffed;
let oldCommit = options._commit;
let oldBeforeUnmount = options.unmount;
let oldRoot = options._root;

const RAF_TIMEOUT = 100;
let prevRaf;
Expand All @@ -35,6 +36,14 @@ options._diff = vnode => {
if (oldBeforeDiff) oldBeforeDiff(vnode);
};

options._root = (vnode, parentDom) => {
if (parentDom._children && parentDom._children._mask) {
vnode._mask = parentDom._children._mask;
}

if (oldRoot) oldRoot(vnode, parentDom);
};

/** @type {(vnode: import('./internal').VNode) => void} */
options._render = vnode => {
if (oldBeforeRender) oldBeforeRender(vnode);
Expand Down
23 changes: 23 additions & 0 deletions hooks/test/browser/useId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,27 @@ describe('useId', () => {
rerender();
expect(first).not.to.equal(scratch.innerHTML);
});

it('should return a unique id across invocations of render', () => {
const Id = () => {
const id = useId();
return <div>My id is {id}</div>;
};

const App = props => {
return (
<div>
<Id />
{props.secondId ? <Id /> : null}
</div>
);
};

render(createElement(App, { secondId: false }), scratch);
expect(scratch.innerHTML).to.equal('<div><div>My id is P0-0</div></div>');
render(createElement(App, { secondId: true }), scratch);
expect(scratch.innerHTML).to.equal(
'<div><div>My id is P0-0</div><div>My id is P0-1</div></div>'
);
});
});

0 comments on commit e1f0d3e

Please sign in to comment.