From 15557fa67fe3cbd4a9bc7bf7340594d3f7e8ab89 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Mon, 28 Nov 2022 09:42:57 -0800 Subject: [PATCH] [Fix] properly track `useId` use in StrictMode in development (#25713) In `` in dev hooks are run twice on each render. For `useId` the re-render pass uses the `updateId` implementation rather than `mountId`. In the update path we don't increment the local id counter. This causes the render to look like no id was used which changes the tree context and leads to a different set of IDs being generated for subsequent calls to `useId` in the subtree. This was discovered here: https://github.com/vercel/next.js/issues/43033 It was causing a hydration error because the ID generation no longer matched between server and client. When strict mode is off this does not happen because the hooks are only run once during hydration and it properly sees that the component did generate an ID. The fix is to not reset the localIdCounter in `renderWithHooksAgain`. It gets reset anyway once the `renderWithHooks` is complete and since we do not re-mount the ID in the `...Again` pass we should retain the state from the initial pass. --- .../src/__tests__/ReactDOMUseId-test.js | 64 +++++++++++++++++++ .../src/ReactFiberHooks.new.js | 1 - .../src/ReactFiberHooks.old.js | 1 - 3 files changed, 64 insertions(+), 2 deletions(-) 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) {