dillon1000/react

Commit

Add useOpaqueIdentifier Hook (#17322)

* Add useOpaqueIdentifier Hook

We currently use unique IDs in a lot of places. Examples are:
  * `<label for="ID">`
  * `aria-labelledby`

This can cause some issues:
  1. If we server side render and then hydrate, this could cause an
     hydration ID mismatch
  2. If we server side render one part of the page and client side
     render another part of the page, the ID for one part could be
     different than the ID for another part even though they are
     supposed to be the same
  3. If we conditionally render something with an ID ,  this might also
     cause an ID mismatch because the ID will be different on other
     parts of the page

This PR creates a new hook `useUniqueId` that generates a different
unique ID based on whether the hook was called on the server or client.
If the hook is called during hydration, it generates an opaque object
that will rerender the hook so that the IDs match.

Co-authored-by: Andrew Clark <[email protected]>
Browse files
Changed paths24 files
First-parent comparison
M packages/react-art/src/ReactARTHostConfig.js ModifiedM packages/react-debug-tools/src/ReactDebugHooks.js ModifiedM packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js ModifiedM packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.internal.js ModifiedM packages/react-dom/src/client/DOMPropertyOperations.js ModifiedM packages/react-dom/src/client/ReactDOMComponent.js ModifiedM packages/react-dom/src/client/ReactDOMHostConfig.js ModifiedM packages/react-dom/src/server/ReactPartialRendererHooks.js ModifiedM packages/react-native-renderer/src/ReactFabricHostConfig.js ModifiedM packages/react-native-renderer/src/ReactNativeHostConfig.js ModifiedM packages/react-reconciler/src/ReactCurrentFiber.js ModifiedM packages/react-reconciler/src/ReactFiberHooks.js ModifiedM packages/react-reconciler/src/ReactFiberHydrationContext.js ModifiedM packages/react-reconciler/src/ReactFiberWorkLoop.js ModifiedM packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js ModifiedM packages/react-test-renderer/src/ReactTestHostConfig.js ModifiedM packages/react/index.classic.fb.js ModifiedM packages/react/index.experimental.js ModifiedM packages/react/index.js ModifiedM packages/react/index.modern.fb.js ModifiedM packages/react/src/React.js ModifiedM packages/react/src/ReactHooks.js ModifiedM packages/shared/ReactSymbols.js ModifiedM scripts/error-codes/codes.json Modified
Patch

Files changed

Rendering syntax-highlighted changes…