useId

If a default is not provided, generate an id.

installyarn add @react-aria/utils
version3.12.0
usageimport {useId} from '@react-aria/utils'

API#


useId( (defaultId?: string )): string

Introduction#


The useId hook creates an autogenerated unique id for an element. An id from props can used instead of the autogenerated id when available.

Example#


import {useId} from '@react-aria/utils';

let elementId = useId();
let componentId = useId(props.id);
import {useId} from '@react-aria/utils';

let elementId = useId();
let componentId = useId(props.id);
import {useId} from '@react-aria/utils';

let elementId = useId();
let componentId = useId(
  props.id
);