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

@rawr51919/react-key-index

v0.1.6

Published

Generate permanent, predictable keys for your React components.

Downloads

40

Readme

react-key-index

Generate stable, unique keys in your React components.

Play with the demo/example here.

react-key-index

What This Component Does

In brief:

  • This package adds a permanent and unique ID to every element in an array.
  • If the parameter is an array of objects, then a unique id will be created for each property.
  • This permanent and unique ID can then be used as a key when iterating over the array in a React component.

Here's a sample of the IDs generated using this node module:

[ 'jRfMcPcj',
  'kRf6cEcn',
  'lYf5cBcl',
  'mZfKc5cv',
  'n5fqcYcz',
  'o2fLcZcB',
  'pYfNcRcR',
  'qxfPcBcJ',
  'rkf0cKcK',
  'v2fjc3cA',
  'wpfzcvcB',
  'xkf4cMcR',
  'yPfzcOcE',
  'zpfpcZcw',
  'ADf3cvcP',
  'BBf9cYcr',
  'DkfKcycp',
  'ERfJcmcr',
  'G6fpcVcQ',
  'J6fmcgc7' ]

Rational For Developing This Package

Use Cases

You know that pesky error message in React telling you to specify a key when iterating over elements? I'm talking about situations like this:

var arr = ["one", "two", "three"];

var list = arr.map((arr) => 
  <li>{arr}</li>
);

Use react-key-index to generate stable, predictable react key IDs. The keys are strings of numbers and letters that don't change when a component mounts or unmounts.

key={index} Is An Antipattern

Using key={index} is an antipattern. Why? Well, if you push a new element into your array your application will display the wrong data because your index/value pairing won't be updated.

Simple Example

You can use keyIndex(arr, 1) to convert an array like ["one", "two", "three"] into:

[
  {
    value: "one",
    id: "kRf6c2fY"
  }, {
    value: "two",
    id: "lYf5cGfM",
  }, {
    value: "three",
    id: "mZfKcGf9"
  }
]

The first parameter in keyIndex(arr, 1) is the array you want to add keys (or IDs) to. The second parameter is a number to uniquely identify your react component or use-case.

Since the generated IDs are stable and predictable, using a different number each time you use the helper function ensures the uniqueness of the generated set of IDs.

Complete Example

import React from 'react';
import React from 'react-dom';
import keyIndex from 'react-key-index';

class App extends React.Component {
  render() {
    var arr = ["one", "two", "three"];
    arr = keyIndex(arr, 1);

    {/* 
    
    keyIndex() will convert arrays into an array of objects 
    with this structure: 
    [
      { id: kRf6c2fY, 
        value: value
      }, {
        ...
      }
    ]
    
    */}

    const list = arr.map((arr) => 
      <li key={arr.id}>{arr.value}</li>
    );
    return(
      <ul>{list}</ul>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

Complex Example

Let's say you want to draw a table with three columns. This use-case is slightly more complicated because you need a unique ID for each column. Therefore you need a unique ID for each object property - a unique ID for each array element will not suffice.

Suppose the data you want to display is formally as follows:

const data = [
  {
    product: 'sneakers',
    price: 20,
    quantity: 100
  }, {
    // more products
  }
]

Then keyIndex(data, 1) will return:

const data = [
  {
     {
      product: 'sneakers',
      _productId: 'kRf6c2fY',
      price: 20,
      _priceId: 'J6fmcgc7',
      quantity: 100,
      _quantityId: 'lYf5cGfM'
    }, {
      ...
    }
  }
]

And you'd use these ids as follows to create your table:

class App extends React.Component {
  render() {
    data = keyIndex(data, 1);
    const table = data.map((data) => 
      <tr>
        <td key={data._productId}>{data.product}</td>
        <td key={data._priceId}>{data.price}</td>
        <td key={data._quantityId}>{data.quantity}</td>
      </tr>
    );

    return(
      <table>
        <tbody>{table}</tbody>
      </table>
    );
  }
}

Install

npm install --save react-key-index

Usage

const keyIndex = require('react-key-index');

keyIndex(arr, 1); 

License

MIT © Ben