Use of useId()
Unanswered
Gray-tailed Tattler posted this in #help-forum
Gray-tailed TattlerOP
Is it generally discouraged to use useId() in client componets? Nearly all the client components where I them are causing hydration errors.
41 Replies
Shot in the dark here, do you have any extensions turned on? That's like 90% of my hydration errors
@Gray-tailed Tattler Is it generally discouraged to use useId() in client componets? Nearly all the client components where I them are causing hydration errors.
Saltwater Crocodile
useId() itself is safe, but hydration issues usually show up when the component tree differs between server and client. In client components this often happens if the number or order of useId() calls changes due to ocnditional rendering, early returns, or list reordering. The IDs are deterministic only when the render structure is identical on both sides.
Longtail tuna
i dont think useId is source of hydration errors u see
Gray-tailed TattlerOP
it was in the error itself
which i could have included here
Plz do
Ragdoll
To answer your question: No, it's not discouraged, but it's tricky. useId is designed to be stable, but if your HTML structure changes between Server and Client (even a tiny bit), React loses track and throws that error.
You probably have some logic wrapped around the component that's causing a mismatch, or you're using a version of Next.js/React where the streaming doesn't sync the ID correctly.
You probably have some logic wrapped around the component that's causing a mismatch, or you're using a version of Next.js/React where the streaming doesn't sync the ID correctly.
Gray-tailed TattlerOP
@Patrick MacDonald
Gray-tailed TattlerOP
const listboxIdRef = useRef(
city-search-${Math.random()) has the same issue@Gray-tailed Tattler may I see the snippet of the component ?
Gray-tailed TattlerOP
Claude already got in there and fixed with this admittedly not super elegant fix
const [listboxId, setListboxId] = useState<string | null>(null)
useEffect(() => {
setListboxId(`city-search-${Math.random().toString(36).slice(2, 9)}`)
}, []) so now it's just making sure to only generate the id in the client and not the server rightThis makes sense because then it would only happen when the component mounts and then it would have to be a client component at that point
Gray-tailed TattlerOP
i come from using Alpine.js pretty heavily and using their $id magic so im in the habit of just adding unique id's to avoid collisions but i'm probably overusing them.
@Gray-tailed Tattler Is it generally discouraged to use useId() in client componets? Nearly all the client components where I them are causing hydration errors.
which components are causing hydration errors?
useId() is supposed to solve hydration error lol
Gray-tailed TattlerOP
for real. i face this with multiple projects
the useId() in my radix component has the same mismatch
@Gray-tailed Tattler Click to see attachment
iirc radix wasn't using useId() up until recently
they had a bug thats commonly reported and when i looked into the components they had used math.random instead of useId()
@Gray-tailed Tattler Click to see attachment
what component is this?
Gray-tailed TattlerOP
Um the form something. Afk
@Gray-tailed Tattler <@1051316149947744266>
Longtail tuna
and it's nextjs/turbopack error
it happens since some v15 canary release
that;s what happens when there is major release every 4/5 months
@Longtail tuna it's bad code
Gray-tailed TattlerOP
What should I do instead?
Longtail tuna
use useId
this error is gone in production so w/e
or leave nextjs and never come back to it
depends
use useId from react, to be more specific
This is a React issue introduced in Next.js 15.5.*. Still present in 16.1.6 afaik. Itās fixed in the latest 16.2.x canary.
It doesnāt just happen with useId, but thatās where it happens most commonly. Sometimes you can suppress it, often itās impossible to suppress. Upgrading or downgrading is the only fix
It doesnāt just happen with useId, but thatās where it happens most commonly. Sometimes you can suppress it, often itās impossible to suppress. Upgrading or downgrading is the only fix
I have never thought of triying with Firefox/Safari š¤
@Alessioš£ This is a React issue introduced in Next.js 15.5.*. Still present in 16.1.6 afaik. Itās fixed in the latest 16.2.x canary.
It doesnāt just happen with useId, but thatās where it happens most commonly. Sometimes you can suppress it, often itās impossible to suppress. Upgrading or downgrading is the only fix
Audubon's Shearwater
yes it seems to be fixed in the 16.1.1-canary.32, but still the stable release (16.1.6) is broken till today.
@aardani use useId from react, to be more specific
Gray-tailed TattlerOP
Yeah I am
@Audubon's Shearwater yes it seems to be fixed in the **16.1.1-canary.32**, but still the stable release (16.1.6) is broken till today.
Gray-tailed TattlerOP
probably a silly thing to upgrade to the canary for right? just wait for a new minor/major release?
@Gray-tailed Tattler probably a silly thing to upgrade to the canary for right? just wait for a new minor/major release?
Audubon's Shearwater
@Alessioš£ what do you think?
Upgrade to canary or stay on 15.4.11 if it was up to me.
Giant panda
hearing useId() for the first time today