GithubHelp home page GithubHelp logo

jrotolo / react-keygen Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 8 KB

Generates unique react keys automatically for component lists

License: MIT License

Makefile 1.23% JavaScript 98.77%
hash fnv-algorithms uniq-key react

react-keygen's Introduction

React Keygen

A small library providing a utility method to list out React components and generate unique keys automatically

Installation

npm install react-keygen --save

Usage

Use reactKeyMap just like you would Array.map. However, no need to worry about finding and passing in a uniq key, reactKeyMap handles that for you!

import React from 'react'
import {reactKeyMap} from 'react-keygen'

const MovieList = ({movies}) => (
  <ul className="MovieList">
    {reactKeyMap(movies, (movie) => <ListItem item={movie} />)}
  </ul>
)

Each <ListItem /> component will have a uniq key created by hashing our data passed in. If you still wish to use your own keys you can still set it like you normally would, and reactKeyMap will default to the user provided key.

Behind the Curtain

The reactKeyMap utility function hashes the first argument you pass into the callback function, usually this will be an object. It passes this hash value in as the key prop by wrapping our normal map callback function in React.cloneElement.

It uses a 32 bit FNV-1 hash algorithm because FNV algorithms are simple, fast, and maintain a low collision rate. FNV hashes are also great at hashing almost identical strings, which is needed since most of the listed data in this context will be very similar.

FNV Hash Algorithm Advantages

  • Fast
  • Low collision rate
  • High dispersion
  • Simple implementation w/ little overhead

For more details see: http://www.isthe.com/chongo/tech/comp/fnv/

Tests

npm test

Release History

  • 0.1.0 Initial release

react-keygen's People

Contributors

jrotolo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.