Skip to content

Commit

Permalink
do not reset localIdCounter on re-running hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Nov 19, 2022
1 parent c08d8b8 commit be6afec
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
64 changes: 64 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMUseId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,68 @@ describe('useId', () => {
</div>
`);
});

// https://github.com/vercel/next.js/issues/43033
// re-rendering in strict mode caused the localIdCounter to be reset but it the rerender hook does not
// increment it again. This only shows up as a problem for subsequent useId's because it affects child
// and sibling counters not the initial one
it('does not forget it mounted an id when re-rendering in dev', async () => {
function Parent() {
const id = useId();
return (
<div>
{id} <Child />
</div>
);
}
function Child() {
const id = useId();
return <div>{id}</div>;
}

function App({showMore}) {
return (
<React.StrictMode>
<Parent />
</React.StrictMode>
);
}

await serverAct(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />);
pipe(writable);
});
expect(container).toMatchInlineSnapshot(`
<div
id="container"
>
<div>
:R0:
<!-- -->
<div>
:R7:
</div>
</div>
</div>
`);

await clientAct(async () => {
ReactDOMClient.hydrateRoot(container, <App />);
});
expect(container).toMatchInlineSnapshot(`
<div
id="container"
>
<div>
:R0:
<!-- -->
<div>
:R7:
</div>
</div>
</div>
`);
});
});
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ function renderWithHooksAgain<Props, SecondArg>(
let children;
do {
didScheduleRenderPhaseUpdateDuringThisPass = false;
localIdCounter = 0;
thenableIndexCounter = 0;

if (numberOfReRenders >= RE_RENDER_LIMIT) {
Expand Down
1 change: 0 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ function renderWithHooksAgain<Props, SecondArg>(
let children;
do {
didScheduleRenderPhaseUpdateDuringThisPass = false;
localIdCounter = 0;
thenableIndexCounter = 0;

if (numberOfReRenders >= RE_RENDER_LIMIT) {
Expand Down

0 comments on commit be6afec

Please sign in to comment.