useId hook
See original GitHub issueUsually for accessibility reasons we need to generate IDs for DOM elements and reference those IDs via e.g. aria-labelledby
attributes on other DOM elements.
Implementing a custom deterministic ID generator is not straightforward:
- If you use random ID generators then most probably IDs will mismatch during hydration, as the IDs generated during hydration will be different than the IDs which were generated on SSR.
- Using a global counter for ID generation might not be SSR-safe, as the counter needs to be unique for each render. Creating a context to store the counter might help tho.
React 18 introduces a useId hook which solves the problem of mismatching IDs on SSR/hydration, as the generated IDs are deterministic and based on the node location in the tree which will match on SSR and on hydration (if I get it right).
I think this would be really useful for most of the Preact users as well. What do you think?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:21
- Comments:7 (3 by maintainers)
Top Results From Across the Web
An Introduction to the useId() Hook in React 18
The useId is a new hook introduced in React 18. The useId hook helps generate a unique Id on both the client-side and...
Read more >useId - React Docs
useId is a React Hook for generating unique IDs that can be passed to accessibility attributes. const id = useId().
Read more >Understanding the useId hook in React - OpenReplay Blog
After much work from the React team, it was renamed to the useId hook. Simply put, the useId hook generates a uniquely stable...
Read more >ReactJS useId Hook - GeeksforGeeks
React useId Hook is introduced for the ReactJS versions above 18. This hook generates unique IDs i.e, returns a string that is stable...
Read more >Understanding How to Use the useId Hook in React 18
React 18 introduced the built-in useId hook which can be used for generating unique ids inside your functional components.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Chara-ui now uses only React 18 and uses useId hook;
To update projects that use chakra-ui and preact that hook is required;
I have a working version in https://github.com/preactjs/preact/pull/3583 just need to update some of the server-side libraries to add support for the
_children
and_parent
properties 😅