You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case of react-async — to maintain a mapping from a component to its state and make it serialisable (to send it over the wire, ES6 Map with components as keys can't be used), in case of react-multiplayer — to have an unique URL per component instance.
Currently both of the libraries use this._rootNodeID + ',' + this._mountDepth. Should this be added to a public API — getComponentID() or something?
this._rootNodeID + ',' + this._mountDepth is redundant even, this._rootNodeID is enough. However, the unique ID is an implementation detail AFAIK, it's likely that it will be removed in the future if/when we ever let go of innerHTML. Also, it's possible that a concatenated ID won't be available soon, but each node will only hold it's own relative ID and possibly a monotonic unique ID for innerHTML (again, implementation detail).
Also this._rootNodeID is only unique at a single point in time, it's not unique over time. I'm unsure why this can't be solved for example via a Mixin that creates a unique ID for the component by just incrementing a global counter.
@syranide_rootNodeID can be shared by multiple composite components, correct?
This seems like something that's fragile -- for cases where you only need a unique ID on one client but don't need it to be consistent across clients, you can make your own autoincrementing counter. For cases where you want the IDs to be consistent across clients, I don't think you can rely on this._rootNodeID because rendering components in a different order (or with server rendering, on different servers) will cause the node IDs to be different -- when you need this I think you really just want to force the person using a mixin to specify a unique key.
@spicyj_rootNodeID must be unique at any point in time, but it only describes the hiearchy of indices and keys, so once a component is removed, the _rootNodeID is likely to be immediately assumed by another component taking it's place.
Anyway, @andreypopp messages me in the chat and it seemed like the Mixin approach was a good solution.
@laser IIRC simply that it was there were better ways to approach the problem that doesn't rely on internals. I have a faint memory of him simply generating a unique ID for each component on mount and using that instead, but I could be way off.
My team is trying to come up with an approach for including a view-specific token with our actions such that errors (say, failing server-side validation) can be linked back to originating views w/out introducing granular, view-specific stores. The approach was suggested by @jingchere. I'd love to be able to rely on a unique id set by React instead of having to generate something myself, if possible.
@laser Yeah, if all you need is a unique ID for each component then you can make a counter yourself and increment it for each new component. (We try to avoid adding functionality to React if it can be easily replicated in component code.)
I don't necessarily like what i'm doing since it's hacky and has a chance of collisions when you have multiple instances on the same page, but I set the state of the component with a random id, and then use that.
Reacted by Randy808, David Kadlec, Shant Parseghian, Nikhil Fadnis, Oliver Askew, Ankit Popli, Adam Drago, Rubia Dias, Nikos Kleidis, littlethings-dev and 3 more
@davidgilbertson Server-rendering, it will work in some trivial setups but for everything beyond that will cause count to become out-of-sync between client and server and cause markup mismatch.
Reacted by George Turkin, Christian Silver, John Fiorilla, Steven Frieson, Luc Hoang Long, Stephen Eckels, Haz, oephrosinin, Alessio Koci, Zhang Yida and 1 more
I used this._reactInternalInstance._debugID for unique classNames for a fancy scrollmagic/gsap effect and when i deployed the whole site shat itself lol
I have a solution that's working pretty well so far. I've been generating sequential ids with a simple utility, and living with the checksums for the time being. This is inspired by what @jwietelmann said above about resetting on a per-request basis but is maybe worth saying again. As he said, this not as much help if you're working on a component library, but works best when you're creating the entire app.
// utility to generate ids
let current = 0
export default function generateId (prefix) {
return `${prefix || 'id'}-${current++}`
}
export function resetIdCounter () { current = 0 }
And then in a root component I call the resetCounter function. On the server, this clears the id from memory each time there's a new server render. In the browser, it doesn't really reset anything, but that's fine.
@mtrabelsi That approach is dangerous because multiple components may be created in under a millisecond. The time resolution isn't high enough.
Still, the technique of assigning ids in the constructor is how I've approached this in the past. However I've used a uuid library for it, and usually prepend a prefix to make the id contextual:
@mtabelsi @nhunzaker, both solutions will not create a persistent id, especially if a certain component is mounted/unmounted across renders (even moving between tabs might unmount a component), not to say between refreshes.
@omerts Correct. It definitely does not fix the issue.
I think this could be a really great feature. Unfortunately I don't really understand enough about Fiber to make it happen, but would happily assist anyone trying to figure this out.
We should probably agree on whether we want this and how it should behave before considering implementations (unless it's just for a proof of concept).
This issue isn't about IDs. It's about whether it's a good idea to preserve state across serialization and if the use cases for that in turn can't be modeled better. Don't get stuck on the ID thing. If serialization is a good idea maybe it's better we just build that in.
Activity
syranide commentedon Feb 20, 2014
this._rootNodeID + ',' + this._mountDepthis redundant even,this._rootNodeIDis enough. However, the unique ID is an implementation detail AFAIK, it's likely that it will be removed in the future if/when we ever let go of innerHTML. Also, it's possible that a concatenated ID won't be available soon, but each node will only hold it's own relative ID and possibly a monotonic unique ID for innerHTML (again, implementation detail).Also
this._rootNodeIDis only unique at a single point in time, it's not unique over time. I'm unsure why this can't be solved for example via a Mixin that creates a unique ID for the component by just incrementing a global counter.sophiebits commentedon Feb 20, 2014
@syranide
_rootNodeIDcan be shared by multiple composite components, correct?This seems like something that's fragile -- for cases where you only need a unique ID on one client but don't need it to be consistent across clients, you can make your own autoincrementing counter. For cases where you want the IDs to be consistent across clients, I don't think you can rely on
this._rootNodeIDbecause rendering components in a different order (or with server rendering, on different servers) will cause the node IDs to be different -- when you need this I think you really just want to force the person using a mixin to specify a unique key.syranide commentedon Feb 20, 2014
@spicyj
_rootNodeIDmust be unique at any point in time, but it only describes the hiearchy of indices and keys, so once a component is removed, the_rootNodeIDis likely to be immediately assumed by another component taking it's place.Anyway, @andreypopp messages me in the chat and it seemed like the Mixin approach was a good solution.
sophiebits commentedon Feb 20, 2014
@syranide I mean if you have two composite components nested with no DOM node in between then they share a rootNodeID:
https://github.com/facebook/react/blob/95edc396dfd07ed064240b5e2e1a1d5528d2d747/src/core/ReactCompositeComponent.js#L839-L843
syranide commentedon Feb 20, 2014
@spicyj Ah right, right,
_rootNodeIDemphasis onNodeas inDOMNode. Excellent point, they're only unique in the DOM (loosely speaking).syranide commentedon May 18, 2014
@spicyj Can close this (this was solved on IRC btw).
laser commentedon Apr 23, 2015
@syranide @spicyj - For those of us who weren't on IRC - what was the resolution?
syranide commentedon Apr 23, 2015
@laser IIRC simply that it was there were better ways to approach the problem that doesn't rely on internals. I have a faint memory of him simply generating a unique ID for each component on mount and using that instead, but I could be way off.
laser commentedon Apr 23, 2015
@syranide Ah, okay. Thanks for clarifying.
My team is trying to come up with an approach for including a view-specific token with our actions such that errors (say, failing server-side validation) can be linked back to originating views w/out introducing granular, view-specific stores. The approach was suggested by @jingc here. I'd love to be able to rely on a unique id set by React instead of having to generate something myself, if possible.
Take care,
Erin
sophiebits commentedon Apr 23, 2015
@laser Yeah, if all you need is a unique ID for each component then you can make a counter yourself and increment it for each new component. (We try to avoid adding functionality to React if it can be easily replicated in component code.)
laser commentedon Apr 23, 2015
@spicyj Cool, makes sense.
Without thinking too hard about it, I could imagine something like:
lemiii commentedon May 8, 2015
I don't necessarily like what i'm doing since it's hacky and has a chance of collisions when you have multiple instances on the same page, but I set the state of the component with a random id, and then use that.
davidgilbertson commentedon Nov 18, 2015
A simple counter and component-specific string works just fine for me.
Is there a scenario where this won't work?
syranide commentedon Nov 18, 2015
@davidgilbertson Server-rendering, it will work in some trivial setups but for everything beyond that will cause
countto become out-of-sync between client and server and cause markup mismatch.22 remaining items
klaygomes commentedon Mar 16, 2017
3 years and nothing yet? What a shame!
jpfiorilla commentedon Jun 30, 2017
I used
this._reactInternalInstance._debugIDfor unique classNames for a fancy scrollmagic/gsap effect and when i deployed the whole site shat itself lolOutThisLife commentedon Jul 7, 2017
+1
sfrieson commentedon Jul 12, 2017
I have a solution that's working pretty well so far. I've been generating sequential ids with a simple utility, and living with the checksums for the time being. This is inspired by what @jwietelmann said above about resetting on a per-request basis but is maybe worth saying again. As he said, this not as much help if you're working on a component library, but works best when you're creating the entire app.
And then in a root component I call the resetCounter function. On the server, this clears the id from memory each time there's a new server render. In the browser, it doesn't really reset anything, but that's fine.
Working well so far.
mtrabelsi commentedon Sep 8, 2017
Actually it's not that complicated to get a unique ID:
As for CSS selector needs the first letter to be a character you can prefix it with an 'id-' on the render method :
Works like a charm, don't wait for React - they have their own policy and list of future pending , maybe will never be implemented :)
nhunzaker commentedon Sep 8, 2017
@mtrabelsi That approach is dangerous because multiple components may be created in under a millisecond. The time resolution isn't high enough.
Still, the technique of assigning ids in the constructor is how I've approached this in the past. However I've used a
uuidlibrary for it, and usually prepend a prefix to make the id contextual:This isn't perfect, and I still like to generate an ID based on props when I can, but it accomplishes the task if prop-based identifiers fail.
jquense commentedon Sep 8, 2017
uuid's in constructors work fine if you don't need SSR, other tho it's likely the id's will mismatch between server and client :/
omerts commentedon Sep 8, 2017
@mtabelsi @nhunzaker, both solutions will not create a persistent id, especially if a certain component is mounted/unmounted across renders (even moving between tabs might unmount a component), not to say between refreshes.
nhunzaker commentedon Sep 8, 2017
@omerts Correct. It definitely does not fix the issue.
I think this could be a really great feature. Unfortunately I don't really understand enough about Fiber to make it happen, but would happily assist anyone trying to figure this out.
sophiebits commentedon Sep 8, 2017
We should probably agree on whether we want this and how it should behave before considering implementations (unless it's just for a proof of concept).
nhunzaker commentedon Sep 8, 2017
@sophiebits Ah sorry, I shouldn't get ahead of myself! I'd love to figure out some resolution here.
sebmarkbage commentedon Sep 9, 2017
This issue isn't about IDs. It's about whether it's a good idea to preserve state across serialization and if the use cases for that in turn can't be modeled better. Don't get stuck on the ID thing. If serialization is a good idea maybe it's better we just build that in.
vorillaz commentedon Apr 1, 2018
I prefer using HOCs where needed in order to easily manipulate nested components.
ruucm commentedon Jul 2, 2018
For 2 Days research, I got a solution for this headache problem.
Here is my solution
Make Unique Id by props in React
Exifers commentedon Jul 3, 2022
Here is a POC, but unfortunately if this hook is called multiple times in the same component, it gives different ids.