<Icon />
component uses custom logic for getting unique ids, that doesn't produce equal results on server and client (in next.js)
#3705
Labels
🐛 bug
Something isn't working
Describe the bug
The
Icon
component uses custom logic for getting unique ids to matcharia-described-by
tags to a<title>
-tag with anid
. This custom id logic doesn't produce equal results on server and client, since it's based onMath.random()
.To track down the bug, let's trek through the source code of the
Icon
component:Assumptions:
The title tag is rendered on Icon.tsx:150:
Let's look for
titleId
on Icon.tsx:138:Now,
count
comes from the call tofindIcon
on Icon.tsx:108:findIcon
, when thedata
prop isn't empty, returns count fromcustomIcon
, as we see on Icon.tsx:57:customIcon
simply returns a random number as itscount
value, which is where the problem comes from:Icon.tsx:50-53
Proposed solution
Use React's
useId
hook (or implement it yourselves), as that is stable between server-side and client-side rendering, allowing us to simply do the following in theIcon
component:Now I do realize that you have to support
react < 18
, so I propose you build a stableuseId
replacement in that case.How about testing something like the following (based on your current
@equinor/eds-utils
hooks/useId.ts
):Steps to reproduce the bug
IconData
object as theicon
prop and a string for thetitle
attribute.The bug results in a warning/error message from next.js in dev mode:
Expected behavior
No error message, as the id's should be equal on server and client.
Specifications
"@equinor/eds-core-react": "^0.42.5",
Additional context
None
The text was updated successfully, but these errors were encountered: