Skip to content

useId

Category
Export Size
299 B
Last Changed
5 days ago

SSR-safe id with an optional static override.

Demo

Usage

tsx
import { 
useId
} from '@reause/core'
function
Field
() {
const
id
=
useId
()
return <
input
id
={
id
} />
}

Pass a string to pin the id instead of generating one:

tsx
import { 
useId
} from '@reause/core'
const
fixedId
=
useId
('my-static-id') // always 'my-static-id'

React ships a hook with the same name, so alias this one when a component needs both: import { useId as useReauseId } from '@reause/core' alongside import { useId as useReactId } from 'react'.

Type Declarations

ts
export function useId(staticId?: string): string

Source

Source · Demo · Mantine

Contributors

hairyf

Changelog

v0.1.6 on
c342f - feat(shared): add useIsomorphicLayoutEffect and adopt it in useId (#923)
45601 - feat(core): add useId (#939)

Released under the MIT License. v0.1.8