All Projects → zzarcon → react-smart-key

zzarcon / react-smart-key

Licence: MIT license
Pass anything as key without re-renders

Programming Languages

typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to react-smart-key

Moses
Utility library for functional programming in Lua
Stars: ✭ 541 (+3082.35%)
Mutual labels:  collection, array
Arrayy
🗃 Array manipulation library for PHP, called Arrayy!
Stars: ✭ 363 (+2035.29%)
Mutual labels:  collection, array
js-deep-sort-object
Simple module to sort objects recursively by its keys
Stars: ✭ 19 (+11.76%)
Mutual labels:  collection, array
Collection
A PHP library for representing and manipulating collections.
Stars: ✭ 488 (+2770.59%)
Mutual labels:  collection, array
Map
PHP Map package for easy and elegant handling of PHP arrays as array-like map objects
Stars: ✭ 1,180 (+6841.18%)
Mutual labels:  collection, array
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (+635.29%)
Mutual labels:  collection, array
go-streams
Stream Collections for Go. Inspired in Java 8 Streams and .NET Linq
Stars: ✭ 127 (+647.06%)
Mutual labels:  collection, array
render react
Pre-render and mount React components from Ruby
Stars: ✭ 14 (-17.65%)
Mutual labels:  render
php-underscore
PHP underscore inspired &/or cloned from _.js, with extra goodies like higher order messaging
Stars: ✭ 42 (+147.06%)
Mutual labels:  collection
wow-classic-items
Collection of all WoW Classic and The Burning Crusade Classic items, professions, zones and classes
Stars: ✭ 29 (+70.59%)
Mutual labels:  items
linqjs
Perform queries on collections in the manner of C#s System.Linq in JavaScript
Stars: ✭ 14 (-17.65%)
Mutual labels:  array
CuVec
Unifying Python/C++/CUDA memory: Python buffered array ↔️ `std::vector` ↔️ CUDA managed memory
Stars: ✭ 73 (+329.41%)
Mutual labels:  array
my-action-github
awesome github
Stars: ✭ 62 (+264.71%)
Mutual labels:  collection
Spice-it-Up
🔥 Anno 1800 Mod Collection
Stars: ✭ 23 (+35.29%)
Mutual labels:  collection
django-secret-key
Generate Django Secret Key
Stars: ✭ 15 (-11.76%)
Mutual labels:  key
pasvl
Array Validator (regular expressions for arrays, sort of)
Stars: ✭ 40 (+135.29%)
Mutual labels:  array
arr
数组增强组件主要是对数组等数据进行处理,如无限级分类操作、商品规格的迪卡尔乘积运算等。
Stars: ✭ 62 (+264.71%)
Mutual labels:  array
arrayfire-haskell
Haskell bindings to ArrayFire
Stars: ✭ 52 (+205.88%)
Mutual labels:  array
DuBLF DuBlast
Quick Playblast tool for Blender
Stars: ✭ 18 (+5.88%)
Mutual labels:  render
velcro
Collection initialization macros for Rust
Stars: ✭ 45 (+164.71%)
Mutual labels:  collection

react-smart-key Build Status

Pass whatever as key without re renders

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.

The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. When iterating over non simple values like promises or class instances, it can be tricky to find the right key for the items while keeping a consistent and predictable renders.

That's what react-smart-key solves.

Install

$ yarn add react-smart-key

Usage

simple

It will keep a global key cache which will last as long as your app runs

import generateKey from 'react-smart-key';

const promise = Promise.resolve(1);
const foo = () => {};

generateKey(promise) === generateKey(promise) // true
generateKey(promise) === generateKey(Promise.resolve(1)) // false
generateKey(foo) === generateKey(foo) // true
generateKey(foo) === generateKey(() => {}) // false

Full

This example illustrates different kind of items and how it will always return the same key for each of them.

import generateKey from 'react-smart-key';

const items = [
  () => {}, 
  Promise.resolve('a'),
  Promise.resolve(1),
  new Date(),
  function a() {},
  1,
  "2"
];
    
class App extends Component {
  render() {
    const list = items.map((item) => <li key={generateKey(item)} />);

    return <ul>{list}</ul>
  }
}

locally

Sometimes, you want to have a per component unique cache. In this case, you can use generateLocalKey which encapsulates a local cache (this also helps garbage collection).

import {generateLocalKey} from 'react-smart-key';
    
class Component1 extends Component {
  constructor() {
    this.generateKey = generateLocalKey();
  }

  render() {
    const list = [1,2,3].map((item) => 
      <li key={this.generateKey(item)} />
    );

    return <ul>{list}</ul>
  }
}

class Component2 extends Component {
  constructor() {
    this.generateKey = generateLocalKey();
  }

  render() {
    const list = ['a', 'b'].map((item) => 
      <li key={this.generateKey(item)} />
    );

    return <ul>{list}</ul>
  }
}

class App extends Component {
  render() {
    return (
      <div>
        <Component1 />
        <Component2 />
      </div>
    )
  }
}

Author

@zzarcon 😜

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].