Gathering detailed insights and metrics for react-stable-uniqueid
Gathering detailed insights and metrics for react-stable-uniqueid
Gathering detailed insights and metrics for react-stable-uniqueid
Gathering detailed insights and metrics for react-stable-uniqueid
A React component that provides a stable unique ID for accessibility, SVG effects, and other purposes
npm install react-stable-uniqueid
Typescript
Module System
Node Version
NPM Version
69.9
Supply Chain
97.3
Quality
74.6
Maintenance
100
Vulnerability
99.6
License
JavaScript (99.29%)
Shell (0.71%)
Total Downloads
28,153
Last Day
3
Last Week
12
Last Month
41
Last Year
1,934
2 Stars
58 Commits
1 Forks
2 Watchers
4 Branches
1 Contributors
Updated on Feb 18, 2022
Minified
Minified + Gzipped
Latest Version
2.0.1
Package Id
react-stable-uniqueid@2.0.1
Unpacked Size
31.40 kB
Size
7.18 kB
File Count
9
NPM Version
8.11.0
Node Version
16.15.1
Cumulative downloads
Total Downloads
Last Day
50%
3
Compared to previous day
Last Week
0%
12
Compared to previous week
Last Month
-65.3%
41
Compared to previous month
Last Year
-48.9%
1,934
Compared to previous year
2
2
22
A utility to generate a unique ID (using Lodash.uniqueId) that does not change on re-renders. This is useful for form inputs (ex: htmlFor
), SVG effects (<defs>
), and other scenarios.
NOTE: This library does not support React 18. Instead, use the built-in useId
hook.
npm install --save react-stable-uniqueid
As a React hook:
1import { useStableUniqueId } from 'react-stable-uniqueid'; 2 3const uniqueId = useStableUniqueId('myPrefix', { 4 uniqueIdFn: myCustomUniqueIdFn, 5});
prefix: string | undefined
Optional. If provided, adds a prefix to the generated unique ID.
uniqueIdFn: () => string
Optional. If provided, uses this function instead of Lodash's to generate a unique ID. Useful for testing.
Note that this function will only be called once; if you update the prop, a new unique ID will not be generated using the new function.
A string value
As a render prop component:
1import { StableUniqueId } from 'react-stable-uniqueid'; 2 3<StableUniqueId render={({ uniqueId }) => <div>Unique ID: {uniqueId}</div>} />;
render: ({ uniqueId: string }) => JSX
A fairly standard render prop. Pass it a function that returns React elements to render given a uniqueId
. This function takes one argument, which is an object of type { uniqueId: string }
- note the destructuring in the examples.
prefix: string
Optional. If provided, adds a prefix to the generated unique ID.
uniqueIdFn: () => string
Optional. If provided, uses this function instead of Lodash's to generate a unique ID. Useful for testing.
Note that this function will only be called once; if you update the prop, a new unique ID will not be generated using the new function.
1import { withStableUniqueId } from 'react-stable-uniqueid'; 2 3const MyComponent = ({ uniqueId }) => <div>Unique ID: {uniqueId}</div>; 4const WrappedMyComponent = withStableUniqueId({ prefix: 'mycomponent' });
uniqueId: string
The generated uniqueId.
prefix: string
Optional. If provided, adds a prefix to the generated unique ID.
name: string
Optional. If provided, renames the uniqueId
prop added to the wrapped component.
uniqueIdFn: () => string
Optional. If provided, uses this function instead of Lodash's to generate a unique ID. Useful for testing.
_uniqueIdFn: () => string
Optional. If provided, uses this function instead of Lodash's to generate a unique ID. Useful for testing. This prop will not be consumed by the wrapper and will also be passed down to the wrapped component.
Note that this function will only be called once; if you update the prop, a new unique ID will not be generated using the new function.
The generated id will not change on re-renders, but it will change if a component instance is unmounted and re-mounted. It is only guaranteed to be stable or unique as long as the component is mounted.
I recommend stubbing this component or using the uniqueIdFn
setting in tests, especially if you're snapshot testing or asserting on the generated, because the default function is not guaranteed to be deterministic.
This component is very likely to generate different IDs on the server vs the client. This means your rendered DOM may be mismatched when you hydrate it on the client. I haven't done much with server-side rendering; maybe this isn't much of a problem. Let me know if you try to do server-side rendering with this library so I can update these docs!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
41 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-12
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More