npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dom-id

v2.2.0

Published

React hooks and HOC to generate unique and deterministic ids for React components

Downloads

10

Readme

react-dom-id

Library to generate unique and deterministic ids for React components.

Install

NPM

npm i --save react-dom-id

Yarn

yarn add react-dom-id

Usage

useId Hook (React Functional Components)

Hook that returns a unique and deterministic id per component or element.

import React from "react";
import { useId } from "react-dom-id";

const RadioButton = ({ children, ...rest }) => {
  const radioButtonId = useId('my-prefix');

  return (
    <div>
      <label htmlFor={radioButtonId}>{children}</label>
      <input id={radioButtonId} type="radio" {...rest} />
    </div>
  );
};

useIdContext Hook (React Functional Components)

Hook that returns some methods provided by the useIdContext. The generateId method could be used to generate unique ids. When we provide a context namespace prefix as param for the useIdContext hook, will override the default namespace prefix, so every time that the generateId is called will be incorporated to the unique ids generated with it.

import React from "react";
import { useIdContext } from "react-dom-id";

const RadioButton = ({ children, ...rest }) => {
  const 
  const { generateId } = useIdContext('my-prefix');
  const id = generateId();

  return (
    <div>
      <label htmlFor={radioButtonId}>{children}</label>
      <input id={id} type="radio" {...rest} />
    </div>
  );
};

withUseIdContext HOC

HOC that integrate some new props to the React Components. The generateId method could be used to generate unique ids. When we provide a context namespace prefix as first param for the withUseIdContext HOC, will override the default namespace prefix, so every time that the generateId is called will be incorporated to the unique ids generated with it.


import React, { Component } from 'react';
import { withUseIdContext } from 'react-dom-id';

class RadioButton extends Component {
  render() {
    const { generateId } = this.props.useIdContext;
    const id = generateId();

    <div>
      <label htmlFor={radioButtonId}>{children}</label>
      <input id={id} type="radio" {...rest} />
    </div>
  }
}

export default withUseIdContext('my-prefix')(RadioButton);

API

useId(namespacePrefix?: string | undefined): string | null;

| Arguments | Type | Required? | Default | Description | |-----------------|----------|---------------|-------------|------------------------------------------| | namespacePrefix | string | No | app | Allows provide a custom namespace prefix |

useIdContext(namespacePrefixContext?: string | undefined): { generateId }

| Arguments | Type | Required? | Default | Description | |-----------------|----------|---------------|-------------|------------------------------------------| | namespacePrefixContext | string | No | app | Allows provide a custom context namespace prefix that will be incorporated to the unique ids |

generateId(namespacePrefix?: string | undefined): string;

| Arguments | Type | Required? | Default | Description | |-----------------|----------|---------------|-------------|------------------------------------------| | namespacePrefix | string | No | app | Allows provide a custom namespace prefix, can be use to override also the custom namespace prefix in the useIdContext hook and withUseIdContext HOC |

withUseIdContext(namespacePrefixContext?: string | undefined): (Component: React.ComponentType): JSX.Element

| Arguments | Type | Required? | Default | Description | |-----------------|----------|---------------|-------------|------------------------------------------| | namespacePrefixContext | string | No | app | Allows provide a custom context namespace prefix that will be incorporated to the unique ids | | Component | React Component | Yes | N/A | Component that will consumer the new feature incorporated by the withUseIdContext HOC |