# react-key-from-object

> Generate react keys from objects

Latest version **1.2.1** (published 2025-07-22) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install react-key-from-object
pnpm add react-key-from-object
yarn add react-key-from-object
bun add react-key-from-object
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2025-07-22 |
| First published | 2021-03-15 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Johan Maupetit |
| Maintainers | poyoman |
| Keywords | react, key, reference, object, map |

## Links

- npm: https://www.npmjs.com/package/react-key-from-object
- Repository: https://github.com/Poyoman39/react-key-from-object
- Homepage: https://github.com/Poyoman39/react-key-from-object#readme
- Issues: https://github.com/Poyoman39/react-key-from-object/issues
- npm.io page: https://npm.io/package/react-key-from-object

## Dependencies (1)

- [react](https://npm.io/package/react.md) >=18.3.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2025-07-22
- 1.2.0 — 2025-07-22
- 1.1.0 — 2023-05-26
- 1.0.3 — 2022-10-25
- 1.0.2 — 2022-10-25
- 1.0.1 — 2022-05-02
- 1.0.0 — 2022-04-27
- 0.0.7 — 2021-06-29
- 0.0.6 — 2021-03-15
- 0.0.5 — 2021-03-15
- 0.0.4 — 2021-03-15
- 0.0.3 — 2021-03-15
- 0.0.2 — 2021-03-15
- 0.0.1 — 2021-03-15

## README

# react-key-from-object

Generate react keys from objects / functions ... anything

## Goal

When using lists in React you need to [use keys](https://reactjs.org/docs/lists-and-keys.html) on items to let React know which array item is associated with which visual.

To do that you have to specify a **unique string** as a *key* prop.

Let's consider you have this array: 

    const dogs = [
      { name: "Perle", age: 2 },
      { name: "Tenor", age: 10 },
      { name: "Perle", age: 12 },
    ];

So we would have a React list looking like this:

    <ul>
      {dogs.map((dog) => (
        <li key={dog.name}>
          {dog.name}
          -
          {dog.age}
        </li>
      ))
    </ul>

The problem here is our dog "names" are not unique ... So we would need to generate a unique ID for every dog.

In some situations, this is tedious ... that's where ***react-key-from-object*** comes in!

    <ul>
      {dogs.map((dog) => (
        <li key={keyGen.getKey(dog)}>
          {dog.name}
          -
          {dog.age}
        </li>
      ))
    </ul>

This will associate a unique key for your object (without mutating it).

Want to know how it works? Have a look to the source code, it's really tiny!

## When NOT to use it

You **MUST NOT** use this library if you need to rely on [react reconciliation](https://reactjs.org/docs/reconciliation.html)
*(So if you are using server side rendering for instance)*

## How to use

### The hook way (recommended)

    import { useKeyGen } from 'react-key-from-object'
    
    const DogList = () => {
      const keyGen = useKeyGen();

      return (
        <ul>
          {dogs.map((dog) => (
            <li key={keyGen.getKey(dog)}>
              {dog.name}
              -
              {dog.age}
            </li>
          ))
        </ul>
      );
    }

### The "classic" way

    import ReactKeyGen from 'react-key-from-object'
    
    const keyGen = new ReactKeyGen()
    
    const t = {};
    const u = {};
    
    keyGen.getKey(t); // keyGen_0
    keyGen.getKey(u); // keyGen_1
    keyGen.getKey(t); // keyGen_0
    keyGen.getKey({}); // keyGen_2

---
_Source: https://npm.io/package/react-key-from-object · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
