Skip to main content

Usage

Returns a stable element id string, unless an explicit id is provided. If no static id is passed, it starts from a React-generated id and switches to a random id after mount to reduce collision risk across separate trees.
import { useId } from '@kuzenbo/hooks';

function Demo() {
  const id = useId();
  return (
    <div>
      <label htmlFor={id}>Email</label>
      <input id={id} type="email" />
    </div>
  );
}

Override with static id

You can provide a static id to override the generated id:
import { useId } from '@kuzenbo/hooks';

function Demo() {
  const id = useId('my-custom-id');
  // id will always be 'my-custom-id'
  return <input id={id} />;
}

Definition

function useId(staticId?: string): string

Parameters

staticId
string
Optional id that overrides generated ids when provided

Returns

id
string
A stable unique identifier string

Build docs developers (and LLMs) love