# @valerie-makes/use-dom-id

> A React Hook for generating unique element IDs

Latest version **0.2.0** (published 2022-01-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @valerie-makes/use-dom-id
pnpm add @valerie-makes/use-dom-id
yarn add @valerie-makes/use-dom-id
bun add @valerie-makes/use-dom-id
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-01-05 |
| First published | 2022-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Valerie Bailey |
| Maintainers | valerie-makes |
| Keywords | react, hook, hooks, a11y, aria, dom |

## Links

- npm: https://www.npmjs.com/package/@valerie-makes/use-dom-id
- Repository: https://github.com/valerie-makes/use-dom-id
- Homepage: https://github.com/valerie-makes/use-dom-id#readme
- Issues: https://github.com/valerie-makes/use-dom-id/issues
- npm.io page: https://npm.io/package/@valerie-makes/use-dom-id

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2022-01-05
- 0.1.1 — 2022-01-04

## README

# use-dom-id

[![Build status](https://github.com/valerie-makes/use-dom-id/actions/workflows/build-test.yml/badge.svg)](https://github.com/valerie-makes/use-dom-id/actions/workflows/build-test.yml) [![MIT License](https://img.shields.io/github/license/valerie-makes/use-dom-id)](https://github.com/valerie-makes/use-dom-id/blob/main/LICENSE) [![NPM version](https://img.shields.io/npm/v/@valerie-makes/use-dom-id)](https://www.npmjs.com/package/@valerie-makes/use-dom-id) [![Known Vulnerabilities](https://snyk.io/test/npm/@valerie-makes/use-dom-id/badge.svg)](https://snyk.io/test/npm/@valerie-makes/use-dom-id)

`use-dom-id` is a dependency-free React Hook for generating unique element IDs. It is compatible with both server-side and client-side rendering, including SSR with client-side hydration. It is particularly useful for HTML forms, where unique IDs are required for labels. It can also be used for other purposes, such as SVG definitions.

## Examples

### Form with single labelled input element

```tsx
import useDomId from "@valerie-makes/use-dom-id";

function MyForm() {
  const [nameInputProps, useNameId] = useDomId<HTMLInputElement>();
  const nameLabelProps = useNameId((id) => ({ htmlFor: id }));

  return (
    <form>
      <label {...nameLabelProps}>Full Name</label>
      <input {...nameInputProps} type="text" />
    </form>
  );
}
```

### Form with multiple labelled input elements

```tsx
import useDomId from "@valerie-makes/use-dom-id";

function MyForm() {
  const [nameInputProps, useNameId] = useDomId<HTMLInputElement>();
  const nameLabelProps = useNameId((id) => ({ htmlFor: id }));

  const [emailInputProps, useEmailId] = useDomId<HTMLInputElement>();
  const emailLabelProps = useEmailId((id) => ({ htmlFor: id }));

  return (
    <form>
      <label {...nameLabelProps}>Full Name</label>
      <input {...nameInputProps} type="text" />

      <label {...emailLabelProps}>Email Address</label>
      <input {...emailInputProps} type="email" />
    </form>
  );
}
```

## Usage

Outputs below are accurate for a server-rendering environment. The prefix `id-client` is used during client-side rendering to avoid collisions, except during hydration, in which IDs provided by the server are preserved.

### `useDomId<E extends Element>()`

Generate a unique ID for a particular type of DOM element.

```ts
import useDomId from "@valerie-makes/use-dom-id";
```

```ts
const [idProps, useId] = useDomId<HTMLInputElement>();

// returns <input id="id-server-0" />
return <input {...idProps} />;
```

### `useDomId<E extends Element>(key: string)`

Generate a unique ID for a particular type of DOM element, using a custom key.

```ts
import useDomId from "@valerie-makes/use-dom-id";
```

```ts
const [idProps, useId] = useDomId<HTMLInputElement>("my-key");

// returns <input id="my-key-server-0" />
return <input {...idProps} />;
```

### `useId<T>(generator: (id: string) => T)`

Using a particular ID, generate props for additional elements.

```ts
const [idProps, useId] = useDomId<HTMLInputElement>();
```

```ts
const labelProps = useId((id) => ({ htmlFor: id }));

// returns <label htmlFor="id-server-0">My Label</label>
return <label {...labelProps}>My Label</label>;
```

### `resetRenderContext()`

Resets render context variables, including the next ID. Useful to run before calling `renderToString()` in a server application such as Express, and before running tests, to ensure predictable outputs.

```ts
import { resetRenderContext } from "@valerie-makes/use-dom-id";
```

```ts
resetRenderContext(); // next ID is now 0
```

## License

Copyright (c) 2022 Valerie Bailey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
_Source: https://npm.io/package/@valerie-makes/use-dom-id · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
