react-use-id-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

react-use-id-hook

This is a tiny hook to create consisent id strings while rendering React, both when doing SSR and normally.

This is useful for creating unique id attributes for DOM elements.

API

<IdProvider [prefix="id-"] />

This provider provides the id state. Your app needs to be wrapped with it.

useId()

This hook returns a unique id that remains consistent on re-render

useGetId()

This hook returns a function that returns unique ids. To use it, the same rule applies as for hooks:

always call it in the same order, without control flow

Example

Also see the example/ folder.

const CheckBox = ({value, onChange}) => {
    const id = useId()
 
    return (
        <>
            <input
                id={id}
                type="checkbox"
                checked={value}
                onChange={ev => onChange(ev.target.checked)}
            />
            <label htmlFor={id}>Click me</label>
        </>
    )
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    429
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    429
  • 1.0.0
    0

Package Sidebar

Install

npm i react-use-id-hook

Weekly Downloads

344

Version

1.0.1

License

MIT

Unpacked Size

13.7 kB

Total Files

11

Last publish

Collaborators

  • wmertens