GithubHelp home page GithubHelp logo

happy-ferret / react-uid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thearnica/react-uid

0.0 2.0 0.0 1.14 MB

Render-less container for generating UID for a11y, consistent react key, and any other good reason ๐Ÿฆ„

License: MIT License

TypeScript 98.37% HTML 1.63%

react-uid's Introduction

UID

Build Status coverage-badge NPM version Greenkeeper badge

Generate UID for an item, Renderless UID containers, SSR-friendly UID generation in 900 bytes.

Example - https://codesandbox.io/s/kkmwr6vv47

API

  • uid(item, [index]) - generates UID for an object(function and so on). Quite usable for React key property. item should be an object, but could be anything. In case it is not an "object", and might have non-unique value - you have to specify second argument - index
 import {uid} from 'react-uid';
 
 // objects
 const data = [{a:1}, {b:2}];
 data.map( item => <li key={uid(item)}>{item}</li>)
 
 // unique strings
 const data = ["a", "b"];
 data.map( item => <li key={uid(item)}>{item}</li>)
 
 // strings
 const data = ["a", "a"];
  data.map( (item, index) => <li key={uid(item, index)}>{item}</li>)
  • UID - renderless container for generation Ids
 import {UID} from 'react-uid';

 <UID>
     {id => (
       <Fragment>
         <input id={id} />
         <label htmlFor={id} />
       </Fragment> 
     )}
 </UID>

 // you can apply some "naming conventions" to the keys
  <UID name={ id => `unique-${id}` }>
      {id => (
        <Fragment>
          <input id={id} />
          <label htmlFor={id} />
        </Fragment>
      )}
  </UID>
  
  // UID also provide `uid` as a second argument
  <UID>
       {(id,uid) => (
         data.map( item => <li key={uid(item)}>{item}</li>) 
       )}
  </UID>
  
  // in the case `item` is not an object, but number or string - provide and index
  <UID>
       {(id,uid) => (
         data.map( (item, index) => <li key={uid(item, index)}>{item}</li>) 
       )}
  </UID>

Server-side friendly UID

  • UIDReset, UIDConsumer, UIDFork - SSR friendly UID. Could maintain consistency across renders. They are much more complex than UID, and provide functionality you might not need.

The key difference - they are not using global "singlentone" to track used IDs, but read it from Context API, thus works without side effects.

Next example will generate the same code, regardless how many time you will render it

 import {UIDReset, UIDConsumer} from 'react-uid';

 <UIDReset>
     <UIDConsumer>
         {(id,uid) => (
           <Fragment>
             <input id={id} />
             <label htmlFor={id} />
             data.map( item => <li key={uid(item)}>{item}</li>)
           </Fragment> 
         )}
     </SmartUID>
 </UIDReset>

UID is not SSR friendly - use UIDConsumer.

Code splitting

Codesplitting may affect the order or existence of the components, so alter the componentDidMount order, and change the generated ID as result.

In case of SPA, this is not something you should be bothered about, but for SSR this could be fatal.

Next example will generate consistent keys regardless of component mount order. Each call to UIDFork creates a new branch of UIDs untangled from siblings.

import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';

 <UIDReset>
     <UIDFork>
      <AsyncLoadedCompoent>
         <UIDConsumer>
           { uid => <span>{uid} is unique </span>}
         </UIDConsumer>
     </UIDFork>
     <UIDFork>
       <AsyncLoadedCompoent>
          <UIDConsumer>
            { uid => <span>{uid} is unique </span>}
          </UIDConsumer>
      </UIDFork>    
 </UIDReset>

# Types
Written in TypeScript

# Licence
 MIT
  

react-uid's People

Contributors

greenkeeper[bot] avatar thearnica avatar thekashey 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.