reactpy icon indicating copy to clipboard operation
reactpy copied to clipboard

use_id hook

Open rmorshea opened this issue 2 years ago • 4 comments
trafficstars

Current Situation

React has a useId hook.

Proposed Actions

Replicate it here (presumably with uuid).

rmorshea avatar May 23 '23 17:05 rmorshea

Hi @rmorshea, I'm new to the open-source community, can I work on this issue?

Paramjeet-singh23 avatar Jun 12 '23 05:06 Paramjeet-singh23

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:

  1. Do not implement use_id in ReactPy because we don't have server-side and client-side components - right now, you can make your own "use_id" hook with use_state(lambda: uuid.uuid4().hex).
  2. Implement a use_id hook but its basically an alias for use_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).
  3. Implement the full use_id algorithm described in the original ReactJS PR - this would be quite complicated.

I'm leaning towards 2, but a second opinion would be helpful.

rmorshea avatar Jun 12 '23 05:06 rmorshea

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.

Archmonger avatar Jun 12 '23 06:06 Archmonger

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.

Archmonger avatar Feb 23 '24 09:02 Archmonger