1.2.0 • Published 2 years ago

@utilityjs/use-deterministic-id v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

A React hook that generates a deterministic unique ID once per component.

license npm latest package npm downloads types

npm i @utilityjs/use-deterministic-id | yarn add @utilityjs/use-deterministic-id

This hook in React versions less than 18 relies on hydration to avoid server/client mismatch errors.\ If you'd like to generate ids server-side, we suggest upgrading to React 18.

Usage

const App = (props) => {
  const id = useDeterministicId();
  // or useDeterministicId(props.id)

  return (
    <div className="app">
      <label htmlFor={id}>Label</label>
      <input type="text" name="text-input" id={id} />
    </div>
  );
};

API

useDeterministicId(idOverride?, prefix?)

declare const useDeterministicId: (idOverride?: string, prefix?: string) => string;

idOverride

Allows you to override the generated id with your own id.

prefix

Allows you to prefix the generated id.