Skip to content

Commit

Permalink
fix(useId): make useId stable for client/server e.g. nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianvitterso committed Dec 12, 2024
1 parent cedfdf0 commit 3e296f1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/eds-utils/src/hooks/useId.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useEffect, useState } from 'react'

let counter = 0

export const useId = (idOverride: string, type?: string): string => {
const [defaultId, setDefaultId] = useState(idOverride)
const id = idOverride || defaultId

useEffect(() => {
if (defaultId == null) {
setDefaultId(
`eds-${type ? type + `-` : ''}${Math.round(Math.random() * 1e5)}`,
)
setDefaultId(`eds-${type ? type + `-` : ''}${counter++}`)
}
}, [defaultId, type])
return id
Expand Down

0 comments on commit 3e296f1

Please sign in to comment.