reactpy
reactpy copied to clipboard
use_id hook
Current Situation
React has a useId hook.
Proposed Actions
Replicate it here (presumably with uuid).
Hi @rmorshea, I'm new to the open-source community, can I work on this issue?
I may have jumped the gun on this one. I'd like to get some feedback from @Archmonger though.
So, after some investigation, useId was introduced in ReactJS 18 specifically to resolve an issue that's unique to server-side components. In particular, React needs an ID generated via useId server-side when "hydrating" (that is, generating the HTML for the initial page load) to be identical to the ID generated via useId by that same component client-side. Reading the original PR, this leads to a lot of complexity in how useId is actually implemented.
TLDR; In ReactJS, useId exists, and is complicated, because it has to deal with server-side and client-side components.
With that in mind. We have a couple options:
- Do not implement
use_idin ReactPy because we don't have server-side and client-side components - right now, you can make your own "use_id" hook withuse_state(lambda: uuid.uuid4().hex). - Implement a
use_idhook but its basically an alias foruse_state(lambda: uuid.uuid4().hex)- users shouldn't be making assumption about how the ID is generated anyway so we could change this later without any issues (ideally). - Implement the full
use_idalgorithm described in the original ReactJS PR - this would be quite complicated.
I'm leaning towards 2, but a second opinion would be helpful.
Any implementation that is just a basic uuid4 wrapper is effectively pointless.
Depending on how we implement nesting multi-level client/server side components, option 3 might end up making sense in the future.
I would say we mark this issue as blocked and re-evaluate between option 1 or 3 later.
I ended up developing a use_root_id hook within ReactPy-Django, as some users were attempting to rely on the root uuid for some database logic.
We may want to propagate an identical implementation to core within the future.