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-list-keys

v1.1.1

Published

A Natural Solution for React list Keys

Downloads

27

Readme

react-list-keys 🔑

"Build Status"

🗝 Predictable, Natural Solution for unique React list keys 🔑

This project was promoted in Why you should love React list keys, and how to crush them, a companion article on Medium.

For certain cases where there is no natural key for an item in a list it can be frustrating to create an artificial key for it. For example, if something hasn't been saved to the server yet and so has no ID, a unique artificial key for each item would have to be created to appease React's list rendering. react-list-keys solves this problem by doing key generation for you.

Note: If your object already has some sort of unique value that could be used as a key for React, it is prefered to use that one. This library's intention is to supplement items without natural keys.

npm install react-list-keys
import React from 'react';
import ReactKeyGen from 'react-list-keys';

class MyComponent extends React.Component {
  state = {
    items: [],
    keyGen: new ReactKeyGen(),
  }

  componentWillMount() {
    this.addItem({ title: 'apple' });
    this.addItem({ title: 'banana' });
  }

  addItem = (item) => {
    this.setState(state => ({
      items: [
        ...state.items,
        state.keyGen.keyed(item), // item with a `_key`
      ]
    }));
  }

  render() {
    return (
      <ul>
        {this.state.items.map(item => (
          <li key={item._key}>{item.title}</li> // use the `_key` as a key
        ))}
      </ul>
    );
  }
}

How it works

react-list-keys adds a _key property to your objects that can be used directly as a key in React lists.

Example:

const keyGen = new ReactKeyGen();
const a = keyGen.keyed({ apples: 1, oranges: 'are great', bananas: null });
a._key // 1

The _key property added to the object is unenumerable. This means that submitting this object via fetch, destructuring the object, etc will not show the _key prop, yet it is there.

const b = { ...a }; // or Object.assign({}, a);
b._key // undefined

While this might seem like a weakness, it's actually very important so that it does not change the structure of your data upon submission or other things.

If you would like to do this object shallow copying, you can do it simply with the provided utility function. In the example above, this would be:

const c = keyGen.copy(a);
c._key // 1
c === a // false

API

react-list-keys exports both an export default class ReactKeyGen for multiple key generation instances for different types of data and a single instance export keyGen for zero-configuration key generation.

// ReactKeyGen can be used to have several key generators for different types of data.
import ReactKeyGen from 'react-list-keys';

// For example on a component-by-component basis or say for a type of data in redux or mobx.
class ListComponent extends React.Component {
  state = {
    items: [],
    myKeyGen: new ReactKeyGen(),
  }

  addItem = (item) => {
    this.setState(state => ({
      items: [
        ...state.items,
        state.keyGen.keyed(item), // item with a `_key`
      ]
    }));
  }

  // ...
}
// Or you can use the exported keyGen, which is simply an instance of ReactKeyGen for
// zero-configuration key generation.
import { keyGen } from 'react-list-keys';

class ListComponent2 extends React.Component {
  state = {
    items: [],
  }

  addItem = (item) => {
    this.setState(state => ({
      items: [
        ...state.items,
        keyGen.keyed(item), // item with a `_key`
      ]
    }));
  }

  // ...
}

Contributing

Submit an issue for any ideas, problems, or suggestions. Pull requests are very welcome in the spirit of open source!