question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usually 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:closed
  • Created 2 years ago
  • Reactions:21
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
Wallacycommented, Jun 23, 2022

Chara-ui now uses only React 18 and uses useId hook;

To update projects that use chakra-ui and preact that hook is required;

6reactions
JoviDeCroockcommented, Jul 4, 2022

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 😅

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found