Skip to content

fix(hooks): generate SSR-safe ids with useId in React 18 - #8320

Open
quinnturner wants to merge 1 commit into
Sage:masterfrom
quinnturner:use-id
Open

quinnturner wants to merge 1 commit into
Sage:masterfrom
quinnturner:use-id

Conversation

@quinnturner

@quinnturner quinnturner commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Proposed behaviour

Generated ids are now produced with React 18's useId, so they are stable between server and client renders and no longer cause hydration mismatches.

A new internal useIdPolyfill hook uses useId when it is available and falls back to the existing per-instance guid behaviour on React 16/17. useUniqueId now uses this hook, which covers the Textbox, TextInput, Date and FileInput components.

Current behaviour

useUniqueId generated ids with useRef(createGuid()), so server-rendered markup and the client's first render used different ids and could trigger hydration warnings or mismatches.

Checklist

  • Commits follow our style guide
  • Related issues linked in commit messages if required
  • Screenshots are included in the PR if useful
  • All themes are supported if required
  • Unit tests added or updated if required
  • Playwright automation tests added or updated if required
  • Storybook added or updated if required
  • Translations added or updated (including creating or amending translation keys table in storybook) if required
  • Typescript d.ts file added or updated if required
  • Related docs have been updated if required

QA

  • Tested in provided StackBlitz sandbox/Storybook
  • Add new Playwright test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

  • React's useId returns values such as :r0: rather than carbon-…. These are valid id and aria-* values, but consumers targeting the old format in CSS selectors may be affected.
  • Other components still call guid() directly via useRef; they can adopt useIdPolyfill incrementally. React 16/17 remains hydration-unsafe without an SSR provider.

Testing instructions

  • npx jest --config=./jest.config.ts --selectProjects Client --testPathPattern 'useIdPolyfill|useUniqueId|textbox/textbox.test|text-input.test'
  • npx tsc --noEmit (only pre-existing Storybook type errors in this environment)

Fixes #8322

@quinnturner
quinnturner requested review from a team as code owners November 27, 2026 18:47
Copilot AI balanced review requested due to automatic review settings November 27, 2026 18:48
@quinnturner
quinnturner marked this pull request as draft November 27, 2026 18:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests cover only client-side rerenders, leaving server-to-client ID stability unverified and unable to detect a React 18 fallback regression that causes hydration mismatches.

1 open issue
What changed in this PR

This PR makes IDs generated by useUniqueId stable across server rendering and hydration on React 18, while preserving the GUID fallback for older React versions.

Changes:

  • Adds an internal useIdPolyfill hook and routes generated IDs and names through it.
  • Adds client-side stability tests for native and fallback ID generation.
  • Updates textbox tests to check generated values and accessibility relationships without assuming a GUID format.
File Description
src/​hooks/​__internal__/​useUniqueId/​useUniqueId.test.tsx Checks generated values without GUID mocks.
src/​hooks/​__internal__/​useUniqueId/​index.ts Uses the new hook for IDs and names.
src/​hooks/​__internal__/​useIdPolyfill/​index.ts Selects React’s useId or the GUID fallback.
src/​hooks/​__internal__/​useIdPolyfill/​index.test.tsx Tests ID stability across client rerenders.
src/​hooks/​__internal__/​useIdPolyfill/​index.fallback.test.tsx Tests fallback GUID stability.
src/​components/​textbox/​textbox.test.tsx Checks generated hint IDs and accessibility references.
src/​components/​textbox/​__internal__/​__next__/​text-input.test.tsx Removes GUID-format assumptions from ID tests.

Review effort: Balanced

@quinnturner
quinnturner marked this pull request as ready for review November 27, 2026 19:09
@quinnturner
quinnturner marked this pull request as draft November 27, 2026 19:10
@quinnturner
quinnturner force-pushed the use-id branch 3 times, most recently from 2694682 to bdd5b18 Compare November 27, 2026 20:07
@quinnturner
quinnturner marked this pull request as ready for review November 27, 2026 20:09
@quinnturner quinnturner changed the title fix(hooks): generate SSR-safe ids with useId fix(hooks): generate SSR-safe ids with useId in React 18 Nov 27, 2026
@edleeks87
edleeks87 self-requested a review December 2, 2026 07:36

@edleeks87 edleeks87 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this @quinnturner, happy to approve this once the FlatTableRow issue is resolved, the other comment is non-blocking for me. The commits will need squashing before merge but it's fine to proceed as is for now

) => {
const internalId = useRef(id ? String(id) : guid());
const generatedId = useIdPolyfill();
const internalId = id ? String(id) : generatedId;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: this will no longer preserve the id like it did before. If you render a subRow without an id then rerender with one passed it loses the styling

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted the implemetation, commented explaining the adjustment, and added a test case preserves generated id when row rendered without id then re-rendered with id to cover this scenario.

@quinnturner
quinnturner force-pushed the use-id branch 10 times, most recently from 94f29d1 to 424e1ea Compare December 3, 2026 13:55
@quinnturner
quinnturner marked this pull request as draft December 3, 2026 14:02
@quinnturner
quinnturner force-pushed the use-id branch 2 times, most recently from 457f47e to e76cd95 Compare December 3, 2026 14:09
@quinnturner
quinnturner marked this pull request as ready for review December 3, 2026 14:13
edleeks87
edleeks87 previously approved these changes Dec 3, 2026
Use React 18's useId when available so generated ids are stable across
server and client renders. React 16/17 keep the previous per-instance
guid behaviour.

Adopt the new useIdPolyfill hook in useUniqueId, which is used by the
Textbox, TextInput, Date and FileInput components.

test: update tests to work with useIdPolyfill migration

test: fix typecheck errors and textarea accessible description for useId migration

test: fix tooltip tests to use screen queries and jest-dom assertions

test: add back snapshot + fix linting

test: re-apply React.useId mocks for deterministic IDs in affected test files

test: strengthen textarea hint id assertions to use mockedGuid

fix(flat-table-row): preserve stable internalId when id changes

Capture the generated/internal id once via useRef so a row first rendered
without an id keeps the same id (and styling) on rerender with an id.
Also add internalId to useEffect dependency arrays to clear lint warnings.

fix(flat-table-row): preserve stable internalId when row re-rendered with id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix(hooks): migrate all remaining useRef(guid()) / useState(guid()) sites to useIdPolyfill for SSR-safe ids

3 participants