@flowsn4ke/usekeys
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

useKeys

Generate unique yet persisted keys for .map() functions in React, allowing React to update only the components that need to be and preventing duplicate keys that lead to bugs. The function it returns accepts the array index as an argument.

Note

You need one instance of useKeys() per .map() function call, otherwise you'll end up with duplicates.

Usage

import useKeys from '@flowsn4ke/usekeys'

export function MyComponent(props) {
  const k = useKeys()

  return <>
  {new Array(42).fill(null).map(value, i) => {
    return <div key={k(i)}>
    unique key number {i}: {k(i)}
    </div>}}
  </>
}

Readme

Keywords

Package Sidebar

Install

npm i @flowsn4ke/usekeys

Weekly Downloads

4

Version

0.1.7

License

MIT

Unpacked Size

9.59 kB

Total Files

10

Last publish

Collaborators

  • flowsn4ke