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

@team-griffin/react-unique-id

v1.1.2

Published

`npm install @team-griffin/react-unique-id --save`

Downloads

283

Readme

react-unique-id

npm install @team-griffin/react-unique-id --save

yarn add @team-griffin/react-unique-id

It gets rather annoying coming up with ids for form controls, as well as when you have to deal with looping over form component. So why not make a react component to do it for us?

UniqueId creates a unique id before the component will be mounted to ensure that the same id is passed down to the component each time. This is to reduce re-renders.

Usage

This package offers unique ids in two flavours, either as a component or a hoc. They both work in the same way. In fact the component version is just a wrapper around the HOC.

<UniqueId/> Component

The component you wish to render can be provided in 3 different forms:

  1. A render function. This is good if you have props that need to be mapped due to the new id prop
  2. A React Component. Use this if you just want an id to be automatically applied to a given component. Avoid passing functions that are created per render in here as React will not be able to determine that they are the same constructor and therefore lots of re-renders
  3. A React Symbol (already created Component). You won't use this often, but is useful if the component you want to give an id has already been created somewhere else in the code

The id that is generated is an auto incrementing integer. You can provide a prefix prop to have prefixed infront of the output.

import UniqueId from '@team-griffin/react-unique-id';

const MyComponentThatNeedsAnId = (props) => (
  <div id={props.id}>
    Hello World!
  </div>
);

// Elsewhere
const MyParentComponent = (props) => {
  // Render method 1
  return (
    <UniqueId render={(props) => {
      <MyComponentThatNeedsAnId
        id={props.id}
      />
    }}/>
  );

  // Render method 2
  return (
    <UniqueId component={MyComponentThatNeedsAnId}/>
  );

  // Render method 3
  const myComp = (
    <MyComponentThatNeedsAnId/>
  );

  return (
    <UniqueId component={myComp}/>
  );
};

withUniqueId HOC

Essentially the same as the component, but doesn't care about rendering. Supports both a runtime prefix and a static prefix. The added benefit of using the HOC implementation is that you can override how the id is generated. This is useful if you want to use a uuid or a ulid instead of an integer. This is only supported a static time. Just provide it into the HOC config under the key generateId. The prefix functionality is still preserved with this enabled.

import { withUniqueId } from '@team-griffin/react-unique-id';

const MyComponentThatNeedsAnId = (props) => (
  <div id={props.id}>
    Hello World!
  </div>
);

const enhanced = withUniqueId({
  prefix: string, // Static prefix, will be used if no runtime one is provided
  generateId: () => string, // Defaults to internal `generateIntId`
})(MyComponentThatNeedsAnId);