Skip to content
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

useId: Use 'H' to separate main id from hook index #23363

Merged
merged 1 commit into from
Feb 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMUseId-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('useId', () => {
}

function normalizeTreeIdForTesting(id) {
const result = id.match(/:(R|r)(.*):(([0-9]*):)?/);
const result = id.match(/:(R|r)([a-z0-9]*)(H([0-9]*))?:/);
if (result === undefined) {
throw new Error('Invalid id format');
}
Expand Down Expand Up @@ -342,7 +342,7 @@ describe('useId', () => {
<div
id="container"
>
:R0:, :R0:1:, :R0:2:
:R0:, :R0H1:, :R0H2:
<!-- -->
</div>
`);
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,16 @@ export function makeId(
): string {
const idPrefix = responseState.idPrefix;

let id = ':' + idPrefix + 'R' + treeId + ':';
let id = ':' + idPrefix + 'R' + treeId;

// Unless this is the first id at this level, append a number at the end
// that represents the position of this useId hook among all the useId
// hooks for this fiber.
if (localId > 0) {
id += localId.toString(32) + ':';
id += 'H' + localId.toString(32);
}

return id;
return id + ':';
}

function encodeHTMLTextNode(text: string): string {
Expand Down
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2072,15 +2072,17 @@ function mountId(): string {
const treeId = getTreeId();

// Use a captial R prefix for server-generated ids.
id = ':' + identifierPrefix + 'R' + treeId + ':';
id = ':' + identifierPrefix + 'R' + treeId;

// Unless this is the first id at this level, append a number at the end
// that represents the position of this useId hook among all the useId
// hooks for this fiber.
const localId = localIdCounter++;
if (localId > 0) {
id += localId.toString(32) + ':';
id += 'H' + localId.toString(32);
}

id += ':';
} else {
// Use a lowercase r prefix for client-generated ids.
const globalClientId = globalClientIdCounter++;
Expand Down
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2072,15 +2072,17 @@ function mountId(): string {
const treeId = getTreeId();

// Use a captial R prefix for server-generated ids.
id = ':' + identifierPrefix + 'R' + treeId + ':';
id = ':' + identifierPrefix + 'R' + treeId;

// Unless this is the first id at this level, append a number at the end
// that represents the position of this useId hook among all the useId
// hooks for this fiber.
const localId = localIdCounter++;
if (localId > 0) {
id += localId.toString(32) + ':';
id += 'H' + localId.toString(32);
}

id += ':';
} else {
// Use a lowercase r prefix for client-generated ids.
const globalClientId = globalClientIdCounter++;
Expand Down