
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
The react-uid package provides utilities for generating unique IDs and managing unique keys in React applications. It helps in ensuring that IDs are unique across the entire application, which is particularly useful for accessibility and dynamic list rendering.
Unique ID Generation
The useUID hook generates a unique ID that can be used for elements in your React component. This is useful for ensuring that IDs are unique across the entire application.
import { useUID } from 'react-uid';
function MyComponent() {
const uid = useUID();
return <div id={uid}>Unique ID Element</div>;
}
UID Forking
The UIDFork component allows you to create a new unique ID context. This is useful when you need to ensure that a set of IDs are unique within a specific part of your application.
import { UIDFork } from 'react-uid';
function MyComponent() {
return (
<UIDFork>
{uid => (
<div id={uid}>Forked Unique ID Element</div>
)}
</UIDFork>
);
}
UID Consumer
The UIDConsumer component provides a unique ID to its child function. This is useful for generating unique IDs in class components or other contexts where hooks cannot be used.
import { UIDConsumer } from 'react-uid';
function MyComponent() {
return (
<UIDConsumer>
{uid => (
<div id={uid}>Consumer Unique ID Element</div>
)}
</UIDConsumer>
);
}
The uuid package is a popular library for generating RFC4122 UUIDs (Universally Unique Identifiers). Unlike react-uid, which is specifically designed for React applications, uuid is a general-purpose library that can be used in any JavaScript environment. It does not provide React-specific utilities like hooks or components.
Nanoid is a small, secure, URL-friendly unique string ID generator for JavaScript. It is similar to uuid in that it is a general-purpose library, but it focuses on being lightweight and fast. Like uuid, it does not provide React-specific utilities.
Shortid is a library for generating short, non-sequential unique IDs. It is designed to be URL-friendly and can be used in any JavaScript environment. While it provides unique ID generation, it does not offer React-specific features like react-uid.
To generate a stable UID/Key for a given item
, consistently between client and server, in 900 bytes.
⚠️ SSR: Not compatible with Strict or Concurent mode. Consider using native useId
(React 18) hook instead.
If your clientside is using StrictMode it will never match SSR-ed Ids due to double invocation
Example - https://codesandbox.io/s/kkmwr6vv47
React UID provides 3 different APIs
uid(item) -> key
<UID>{ id => <><label htmlFor={id}/><input id={id}/></>}</UID>
useUID
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,
UID
- renderless container for generation IdsUIDConsumer
- 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,
useUID()
will generate a "stable" UIDuseUIDSeed()
will generate a seed generator, you can use for multiple fieldsimport { 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,
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.
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.
"Basic API" is not using Context API to keep realization simple, and React tree more flat.
Written in TypeScript
MIT
FAQs
Render-less container for ID generation
The npm package react-uid receives a total of 300,327 weekly downloads. As such, react-uid popularity was classified as popular.
We found that react-uid demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.