All Projects → thearnica → React Uid

thearnica / React Uid

Licence: mit
Render-less container for generating UID for a11y, consistent react key, and any other good reason 🦄

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to React Uid

Svelte Navigator
Simple, accessible routing for Svelte
Stars: ✭ 125 (-48.77%)
Mutual labels:  a11y, ssr
Accessibility interview questions
A starting point for questions to ask someone that wants you to give them a job
Stars: ✭ 236 (-3.28%)
Mutual labels:  a11y
React Pwa
An upgradable boilerplate for Progressive web applications (PWA) with server side rendering, build with SEO in mind and achieving max page speed and optimized user experience.
Stars: ✭ 2,433 (+897.13%)
Mutual labels:  ssr
Get Free Ss
【停止维护】Node爬虫学习:自动爬取网络上公开的免费 SS\SSR 账号密码,并替换掉软件中旧的账号。
Stars: ✭ 224 (-8.2%)
Mutual labels:  ssr
React Rendering Strategies
Improve your React ⚛️ app performance by using Dynamic Rendering, Progressive Rendering or Static Rendering
Stars: ✭ 217 (-11.07%)
Mutual labels:  ssr
Wai Tutorials
W3C WAI’s Web Accessibility Tutorials
Stars: ✭ 229 (-6.15%)
Mutual labels:  a11y
Firebase Functions Next Example
Host a Next.js SSR React app on Cloud Functions for Firebase with Firebase Hosting
Stars: ✭ 215 (-11.89%)
Mutual labels:  ssr
Handorgel
Accessible W3C conform accordion written in ES6.
Stars: ✭ 239 (-2.05%)
Mutual labels:  a11y
Angular11 App
Angular 11 ,Bootstrap 5, Node.js, Express.js, CRUD REST API, PWA, SSR, SEO, Angular Universal, Lazy Loading, PostgreSQL, MYSQL
Stars: ✭ 233 (-4.51%)
Mutual labels:  ssr
Proxysu
Xray,V2ray,Trojan,NaiveProxy, Trojan-Go, ShadowsocksR(SSR),Shadowsocks-libev及相关插件,MTProto+TLS 一键安装工具,windows下用(一键科学上网)
Stars: ✭ 3,309 (+1256.15%)
Mutual labels:  ssr
Clashr Auto Desktop
use clashr & tun2socks
Stars: ✭ 201 (-17.62%)
Mutual labels:  ssr
Fastify Nextjs
React server side rendering support for Fastify with Next
Stars: ✭ 216 (-11.48%)
Mutual labels:  ssr
Use Ssr
☯️ React hook to determine if you are on the server, browser, or react native
Stars: ✭ 230 (-5.74%)
Mutual labels:  ssr
Hydux
A light-weight type-safe Elm-like alternative for Redux ecosystem, inspired by hyperapp and Elmish
Stars: ✭ 216 (-11.48%)
Mutual labels:  ssr
Shadowsocksrss
shadowsocksR backup, windows / android / mac downloads, source code backup
Stars: ✭ 236 (-3.28%)
Mutual labels:  ssr
Hapi Universal Redux
DEPRECATED: Create an universal React and Redux app in less than 5 minutes!
Stars: ✭ 215 (-11.89%)
Mutual labels:  ssr
Next Blog Firestore
Example of blog built with React, Next.js, Firebase Firestore, Styled-Component, Mobx State Tree and other cool technologies
Stars: ✭ 219 (-10.25%)
Mutual labels:  ssr
Ssr
React Server-Side Rendering Example
Stars: ✭ 226 (-7.38%)
Mutual labels:  ssr
Focus Layers
Tiny React hooks for isolating focus within subsections of the DOM.
Stars: ✭ 238 (-2.46%)
Mutual labels:  a11y
Freedom
一个小白对于科学上网的一些切身感受的整理,自己捋思路,同时也为方便他人。发现错误的地方欢迎斧正。顺便也会不断整理一些实用资源及工具。
Stars: ✭ 236 (-3.28%)
Mutual labels:  ssr

UID

Build Status coverage-badge NPM version Greenkeeper badge bundle size downloads

To generate a stable UID/Key for a given item, consistently between client and server, in 900 bytes.

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

API

React UID provides 3 different APIs

  • vanilla js API - uid(item) -> key
  • React Component, via renderProp based API - <UID>{ id => <><label htmlFor={id}/><input id={id}/></>}</UID>
  • React Hooks - useUID

Javascript

  • uid(item, [index]) - generates UID for an object(array, function and so on), result could be used as React key. 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>)

JS API might be NOT (multi-tenant)SSR friendly,

React Components

  • (deprecated)UID - renderless container for generation Ids
  • UIDConsumer - 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>
       {(_, 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>
       {(_, uid) => (
         data.map( (item, index) => <li key={uid(item, index)}>{item}</li>) 
       )}
  </UID>

The difference between uid and UID versions are in "nesting" - any UID used inside another UID would contain "parent prefix" in the result, scoping uid to the local tree branch.

UID might be NOT SSR friendly,

Hooks (16.8+)

  • useUID() will generate a "stable" UID
  • useUIDSeed() will generate a seed generator, you can use for multiple fields
import { useUID, useUIDSeed } from 'react-uid';

const Form = () => {
  const uid = useUID();  
  return (
    <>
     <label htmlFor={uid}>Email: </label>
     <input id={uid} name="email" />
    </>
  )
}

const Form = () => {
  const seed = useUIDSeed();  
  return (
    <>
     <label htmlFor={seed('email')}>Email: </label>
     <input id={seed('email')} name="email" />
     {data.map(item => <div key={seed(item)}>...</div>
    </>
  )
}

Hooks API is SSR friendly,

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> 
         )}
     </UIDConsumer>
 </UIDReset>

UID is not 100% 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>

The hooks API only needs the <UIDFork> wrapper.

So hard?

"Basic API" is not using Context API to keep realization simple, and React tree more flat.

Types

Written in TypeScript

Licence

MIT

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].