diff --git a/packages/react-dom/src/__tests__/ReactDOMUseId-test.js b/packages/react-dom/src/__tests__/ReactDOMUseId-test.js index 031ef39360c31..4ef6ae8007615 100644 --- a/packages/react-dom/src/__tests__/ReactDOMUseId-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMUseId-test.js @@ -633,4 +633,68 @@ describe('useId', () => { `); }); + + // 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 ( +
+ {id} +
+ ); + } + function Child() { + const id = useId(); + return
{id}
; + } + + function App({showMore}) { + return ( + + + + ); + } + + await serverAct(async () => { + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(); + pipe(writable); + }); + expect(container).toMatchInlineSnapshot(` +
+
+ :R0: + + +
+ :R7: +
+
+
+ `); + + await clientAct(async () => { + ReactDOMClient.hydrateRoot(container, ); + }); + expect(container).toMatchInlineSnapshot(` +
+
+ :R0: + + +
+ :R7: +
+
+
+ `); + }); }); diff --git a/packages/react-reconciler/src/ReactFiberHooks.new.js b/packages/react-reconciler/src/ReactFiberHooks.new.js index 3f97d0e8639c0..a3c9eefb9cb37 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.new.js +++ b/packages/react-reconciler/src/ReactFiberHooks.new.js @@ -697,7 +697,6 @@ function renderWithHooksAgain( let children; do { didScheduleRenderPhaseUpdateDuringThisPass = false; - localIdCounter = 0; thenableIndexCounter = 0; if (numberOfReRenders >= RE_RENDER_LIMIT) { diff --git a/packages/react-reconciler/src/ReactFiberHooks.old.js b/packages/react-reconciler/src/ReactFiberHooks.old.js index 6c272d6b0613d..1917a76b09a29 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.old.js +++ b/packages/react-reconciler/src/ReactFiberHooks.old.js @@ -697,7 +697,6 @@ function renderWithHooksAgain( let children; do { didScheduleRenderPhaseUpdateDuringThisPass = false; - localIdCounter = 0; thenableIndexCounter = 0; if (numberOfReRenders >= RE_RENDER_LIMIT) {