Socket
Book a DemoInstallSign in
Socket

@charlietango/use-id

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@charlietango/use-id

Generate a deterministic id using a Context Provider

latest
Source
npmnpm
Version
1.10.0
Version published
Weekly downloads
40
207.69%
Maintainers
2
Weekly downloads
 
Created
Source

useId

Generate a deterministic id using a ContextProvider. Use this as a way of generating unique ids for input fields, or linking aria fields.

Checkout the Storybook demo.

Installation

yarn add @charlietango/use-id

API

const id = useId(prefix?: string)

The useId hook should ideally be wrapped with the IdProvider. This ensures that the generated ids are deterministic, since the provider gets created once for each instance of the application.

The hook will revert to generating the id as a side effect if the IdProvider is not included. This means you will get back undefined during the initial render.

⚠️ When using the hook to generate an id for an HTML element, make sure you prefix it with fixed string value - otherwise you'll create a global id that's just a number.

Example

import React from 'react'
import useId, { IdProvider } from '@charlietango/use-id'

const LoginForm = () => {
  const id = useId()
  const inputId = `input-${id}`
  const passwordId = `pass-${id}`

  return (
    <form>
      <label htmlFor={inputId}>Username:</label>
      <input id={inputId} />
      <label htmlFor={passwordId}>Password:</label>
      <input
        type="password"
        id={passwordId}
        aria-describedby={passwordId + '-desc'}
      />
      <p role="alert" id={passwordId + '-desc'}>
        Please enter a password.
      </p>
    </form>
  )
}

const App = () => {
  return (
    <IdProvider>
      <LoginForm />
    </IdProvider>
  )
}

export default App

Keywords

react

FAQs

Package last updated on 14 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts