Skip to main content
react-md

useEnsuredId

function useEnsuredId(propId: string | undefined, prefix: string): string;

This hook is used to ensure that an id has been provided to a component either through props or use the useId hook.

Example Usage

import { useEnsuredId } from "@react-md/core/useEnsuredId";
import type { HTMLAttributes, ReactElement } from "react";

export function MaterialDesignComponent(
  props: HTMLAttributes<HTMLDivElement>
): ReactElement {
  const id = useEnsuredId(props.id, "component-name");

  return <div {...props} id={id} />;
}

Parameters

Returns

An id to be passed to an element; generally for accessibility.