{"id":16401389,"url":"https://github.com/thearnica/react-uid","last_synced_at":"2025-05-14T19:03:24.448Z","repository":{"id":32513983,"uuid":"135665108","full_name":"thearnica/react-uid","owner":"thearnica","description":"Render-less container for generating UID for a11y, consistent react key, and any other good reason 🦄","archived":false,"fork":false,"pushed_at":"2025-01-11T01:41:41.000Z","size":1699,"stargazers_count":304,"open_issues_count":26,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T00:04:53.210Z","etag":null,"topics":["a11y","react-context","ssr","uid"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thearnica.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-01T03:53:23.000Z","updated_at":"2025-01-11T01:41:44.000Z","dependencies_parsed_at":"2024-01-16T04:31:52.611Z","dependency_job_id":"4240d118-8656-4b4d-985e-3eba225fc5c1","html_url":"https://github.com/thearnica/react-uid","commit_stats":{"total_commits":47,"total_committers":8,"mean_commits":5.875,"dds":0.574468085106383,"last_synced_commit":"bee9b70354c299a493ca9cafd46b3300ead6721a"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearnica%2Freact-uid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearnica%2Freact-uid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearnica%2Freact-uid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thearnica%2Freact-uid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thearnica","download_url":"https://codeload.github.com/thearnica/react-uid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248663189,"owners_count":21141708,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["a11y","react-context","ssr","uid"],"created_at":"2024-10-11T05:30:18.133Z","updated_at":"2025-04-14T02:58:08.709Z","avatar_url":"https://github.com/thearnica.png","language":"TypeScript","readme":"# UID\n\n[![Build Status](https://travis-ci.org/thearnica/react-uid.svg?branch=master)](https://travis-ci.org/thearnica/react-uid)\n[![coverage-badge](https://img.shields.io/codecov/c/github/thearnica/react-uid.svg?style=flat-square)](https://codecov.io/github/thearnica/react-uid)\n[![NPM version](https://img.shields.io/npm/v/react-uid.svg)](https://www.npmjs.com/package/react-uid)\n[![Greenkeeper badge](https://badges.greenkeeper.io/thearnica/react-uid.svg)](https://greenkeeper.io/)\n[![bundle size](https://badgen.net/bundlephobia/minzip/react-uid)](https://bundlephobia.com/result?p=react-uid)\n[![downloads](https://badgen.net/npm/dm/react-uid)](https://www.npmtrends.com/react-uid)\n\nTo generate a _stable_ UID/Key for a given `item`, consistently between client and server, **in 900 bytes**.\n\n⚠️ SSR: **Not compatible with Strict or Concurent mode**. Consider using _native_ `useId`(React 18) hook instead.\n\n\u003e If your clientside is using StrictMode it will never match SSR-ed Ids due to double invocation\n\nExample - https://codesandbox.io/s/kkmwr6vv47\n\n## API\n\nReact UID provides 3 different APIs\n\n- vanilla js API - `uid(item) -\u003e key`\n- React Component, via renderProp based API - `\u003cUID\u003e{ id =\u003e \u003c\u003e\u003clabel htmlFor={id}/\u003e\u003cinput id={id}/\u003e\u003c/\u003e}\u003c/UID\u003e`\n- React Hooks - `useUID`\n\n#### Javascript\n\n- `uid(item, [index])` - generates UID for an object(array, function and so on), result could be used as React `key`.\n  `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`\n\n```js\nimport { uid } from 'react-uid';\n\n// objects\nconst data = [{ a: 1 }, { b: 2 }];\ndata.map((item) =\u003e \u003cli key={uid(item)}\u003e{item}\u003c/li\u003e);\n\n// unique strings\nconst data = ['a', 'b'];\ndata.map((item) =\u003e \u003cli key={uid(item)}\u003e{item}\u003c/li\u003e);\n\n// strings\nconst data = ['a', 'a'];\ndata.map((item, index) =\u003e \u003cli key={uid(item, index)}\u003e{item}\u003c/li\u003e);\n```\n\nJS API might be NOT (multi-tenant)**SSR friendly**,\n\n#### React Components\n\n- (deprecated)`UID` - renderless container for generation Ids\n- `UIDConsumer` - renderless container for generation Ids\n\n```js\n import {UID} from 'react-uid';\n\n \u003cUID\u003e\n     {id =\u003e (\n       \u003cFragment\u003e\n         \u003cinput id={id} /\u003e\n         \u003clabel htmlFor={id} /\u003e\n       \u003c/Fragment\u003e\n     )}\n \u003c/UID\u003e\n\n // you can apply some \"naming conventions\" to the keys\n  \u003cUID name={ id =\u003e `unique-${id}` }\u003e\n      {id =\u003e (\n        \u003cFragment\u003e\n          \u003cinput id={id} /\u003e\n          \u003clabel htmlFor={id} /\u003e\n        \u003c/Fragment\u003e\n      )}\n  \u003c/UID\u003e\n\n  // UID also provide `uid` as a second argument\n  \u003cUID\u003e\n       {(_, uid) =\u003e (\n         data.map( item =\u003e \u003cli key={uid(item)}\u003e{item}\u003c/li\u003e)\n       )}\n  \u003c/UID\u003e\n\n  // in the case `item` is not an object, but number or string - provide and index\n  \u003cUID\u003e\n       {(_, uid) =\u003e (\n         data.map( (item, index) =\u003e \u003cli key={uid(item, index)}\u003e{item}\u003c/li\u003e)\n       )}\n  \u003c/UID\u003e\n```\n\nThe 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.\n\nUID might be NOT **SSR friendly**,\n\n#### Hooks (16.8+)\n\n- `useUID()` will generate a \"stable\" UID\n- `useUIDSeed()` will generate a seed generator, you can use for multiple fields\n\n```js\nimport { useUID, useUIDSeed } from 'react-uid';\n\nconst Form = () =\u003e {\n  const uid = useUID();\n  return (\n    \u003c\u003e\n     \u003clabel htmlFor={uid}\u003eEmail: \u003c/label\u003e\n     \u003cinput id={uid} name=\"email\" /\u003e\n    \u003c/\u003e\n  )\n}\n\nconst Form = () =\u003e {\n  const seed = useUIDSeed();\n  return (\n    \u003c\u003e\n     \u003clabel htmlFor={seed('email')}\u003eEmail: \u003c/label\u003e\n     \u003cinput id={seed('email')} name=\"email\" /\u003e\n     {data.map(item =\u003e \u003cdiv key={seed(item)}\u003e...\u003c/div\u003e\n    \u003c/\u003e\n  )\n}\n```\n\nHooks API **is SSR friendly**,\n\n### Server-side friendly UID\n\n- `UIDReset`, `UIDConsumer`, `UIDFork` - SSR friendly UID. Could maintain consistency across renders.\n  They are much more complex than `UID`, and provide functionality you might not need.\n\nThe key difference - they are not using global \"singlentone\" to track used IDs,\nbut read it from Context API, thus works without side effects.\n\nNext example will generate the same code, regardless how many time you will render it\n\n```js\nimport { UIDReset, UIDConsumer } from 'react-uid';\n\n\u003cUIDReset\u003e\n  \u003cUIDConsumer\u003e\n    {(id, uid) =\u003e (\n      \u003cFragment\u003e\n        \u003cinput id={id} /\u003e\n        \u003clabel htmlFor={id} /\u003e\n        data.map( item =\u003e \u003cli key={uid(item)}\u003e{item}\u003c/li\u003e)\n      \u003c/Fragment\u003e\n    )}\n  \u003c/UIDConsumer\u003e\n\u003c/UIDReset\u003e;\n```\n\n**UID** is not 100% SSR friendly - use **UIDConsumer**.\n\n### Code splitting\n\nCodesplitting may affect the order or existence of the components, so alter\nthe `componentDidMount` order, and change the generated ID as result.\n\nIn case of SPA, this is not something you should be bothered about, but for SSR\nthis could be fatal.\n\nNext example will generate consistent keys regardless of component mount order.\nEach call to `UIDFork` creates a new branch of UIDs untangled from siblings.\n\n```js\nimport {UIDReset, UIDFork, UIDConsumer} from 'react-uid';\n\n \u003cUIDReset\u003e\n     \u003cUIDFork\u003e\n      \u003cAsyncLoadedCompoent\u003e\n         \u003cUIDConsumer\u003e\n           { uid =\u003e \u003cspan\u003e{uid} is unique \u003c/span\u003e}\n         \u003c/UIDConsumer\u003e\n     \u003c/UIDFork\u003e\n     \u003cUIDFork\u003e\n       \u003cAsyncLoadedCompoent\u003e\n          \u003cUIDConsumer\u003e\n            { uid =\u003e \u003cspan\u003e{uid} is unique \u003c/span\u003e}\n          \u003c/UIDConsumer\u003e\n      \u003c/UIDFork\u003e\n \u003c/UIDReset\u003e\n```\n\nThe hooks API only needs the `\u003cUIDFork\u003e` wrapper.\n\n### So hard?\n\n\"Basic API\" is not using Context API to keep realization simple, and React tree more flat.\n\n# Types\n\nWritten in TypeScript\n\n# Licence\n\nMIT\n","funding_links":[],"categories":["TypeScript","⚛️ React"],"sub_categories":["React-specific libs:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthearnica%2Freact-uid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthearnica%2Freact-uid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthearnica%2Freact-uid/lists"}