Skip to content

[Fix] properly track useId use in StrictMode in development #25713

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

Merged
merged 1 commit into from
Nov 28, 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
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