useId

Generate a random ID for memory
Import
import { useId } from '@kubed/hooks;'
Source
View source code
Docs
Edit this page
npm
@kubed/hooks

Usage

useId generates a random ID that persists during rendering. Hooks are typically used to bind input elements to tags. The generated random id is saved into the ref and will not change unless the component is unloaded.

API

The useId hook accepts two parameters:

  • id – the string value returned by default
  • generateId – function for generating random IDs

Hook returns a string value, which can be either "id" (if the first argument is passed) or a random id generated using the "generateId" function.

1
function useId(id: string, generateId?: () => string): string;

Params

ParametersDefault ValueTypeDescription
id-stringThe string value returned by default
generateId-() => stringFunction to generate a random ID`

Result

ParametersDefault ValueTypeDescription
id-stringGenerate a random ID for memory