Need help with react-uid?
Click the ‚Äúchat‚ÄĚ button below for chat support from the developer who created it, or find similar developers for support.

About the developer

thearnica
237 Stars 11 Forks MIT License 61 Commits 18 Opened issues

Description

Render-less container for generating UID for a11y, consistent react key, and any other good reason ūü¶Ą

Services available

!
?

Need anything else?

Contributors list

# 326,642
TypeScr...
HTML
react-c...
React
17 commits
# 17,164
React
HTML
JavaScr...
Webpack
15 commits
# 176
nextjs
postman...
graphql...
angular...
5 commits
# 748
GitHub
rust-la...
wechat-...
herokua...
2 commits
# 20,221
bootstr...
nix
express...
macos-s...
1 commit
# 214,251
HTML
Shell
findbug...
Kotlin
1 commit
# 49,373
travis-...
yarn
Django
Less
1 commit
# 263,516
react-c...
Dart
Flutter
Android
1 commit

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 -
{ id => <>
- 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
    ```js import {uid} from 'react-uid';

// objects const data = [{a:1}, {b:2}]; data.map( item =>

  • {item}
  • )

    // unique strings const data = ["a", "b"]; data.map( item =>

  • {item}
  • )

    // strings const data = ["a", "a"]; data.map( (item, index) =>

  • {item}
  • ) ```

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

    React Components

    • (deprecated)
      UID
      - renderless container for generation Ids
    • UIDConsumer
      - renderless container for generation Ids ```js import {UID} from 'react-uid';

    {id => ( )}

    // you can apply some "naming conventions" to the keys

    unique-${id}
    }> {id => ( )}

    // UID also provide

    uid
    as a second argument {(_, uid) => ( data.map( item =>
  • {item}
  • ) )}

    // in the case

    item
    is not an object, but number or string - provide and index {(_, uid) => ( data.map( (item, index) =>
  • {item}
  • ) )} ``
    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 ```js import { useUID, useUIDSeed } from 'react-uid';

    const Form = () => { const uid = useUID();
    return ( <> </> ) }

    const Form = () => { const seed = useUIDSeed();
    return ( <> {data.map(item =>

    ...
    </> ) } ``` 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 ```js import {UIDReset, UIDConsumer} from 'react-uid';

    {(id,uid) => ( data.map( item =>

  • {item}
  • ) )} ```

    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. ```js import {UIDReset, UIDFork, UIDConsumer} from 'react-uid';

    { uid => {uid} is unique } { uid => {uid} is unique }
    ```

    The hooks API only needs the

     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

    We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.