Repository navigation
fix(hooks): generate SSR-safe ids with useId in React 18 - #8320
quinnturner wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 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
useIdPolyfillhook 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
2694682 to
bdd5b18
Compare
edleeks87
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
94f29d1 to
424e1ea
Compare
457f47e to
e76cd95
Compare
6a84c41 to
c35cecf
Compare
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

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
useIdPolyfillhook usesuseIdwhen it is available and falls back to the existing per-instance guid behaviour on React 16/17.useUniqueIdnow uses this hook, which covers theTextbox,TextInput,DateandFileInputcomponents.Current behaviour
useUniqueIdgenerated ids withuseRef(createGuid()), so server-rendered markup and the client's first render used different ids and could trigger hydration warnings or mismatches.Checklist
d.tsfile added or updated if requiredQA
Additional context
useIdreturns values such as:r0:rather thancarbon-…. These are valididandaria-*values, but consumers targeting the old format in CSS selectors may be affected.guid()directly viauseRef; they can adoptuseIdPolyfillincrementally. 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