preact icon indicating copy to clipboard operation
preact copied to clipboard

useId hook

Open dios-david opened this issue 3 years ago • 4 comments

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?

dios-david avatar Dec 10 '21 02:12 dios-david

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

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

Wallacy avatar Jun 23 '22 14:06 Wallacy

Same with Reach UI. Running into errors when trying to switch to using preact/compat.

kmcaloon avatar Jul 04 '22 12:07 kmcaloon

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 😅

JoviDeCroock avatar Jul 04 '22 12:07 JoviDeCroock

An issue that seems very apparent now coming to think about it is that a large amount of users use React on the server and Preact on the client 😅

JoviDeCroock avatar Jul 08 '22 16:07 JoviDeCroock

@JoviDeCroock will you cut a new release soon with these changes?

mjgerace avatar Sep 06 '22 22:09 mjgerace

@mjgerace https://github.com/preactjs/preact/pull/3583#issuecomment-1236406632 sorry but this is really getting on my nerves - we as a team have been busy with releasing signals which was a big task 😅 I am trying my very best to get to everyone but.... it's not as easy as it might look

JoviDeCroock avatar Sep 06 '22 22:09 JoviDeCroock

@JoviDeCroock my apologies! I was just wondering about timeline - as you know, this will enable many folks to use React 18 component libraries safely. Please take the comment more as eagerness to be able to use new features rather than an attempt to annoy or beg. Take your time :)

mjgerace avatar Sep 06 '22 23:09 mjgerace