provide-id-gen
v1.0.0
Published
Tiny provider factory for generating IDs.
Downloads
11
Maintainers
Readme
provide-id-gen
Tiny provider factory for generating IDs.
Table of contents
Installation
npm install provide-id-gen --save
Usage
Pass any set of keys (an array of strings) to the provideIdGen
function and it will return a provider with genId
action(s) and newId
reducer(s) based on each key. Each key is usually some provider key for which you would like to generate IDs.
Example
// src/providers/idGen.js
import provideIdGen from 'provide-id-gen';
const idGen = provideIdGen([
'foo',
'bar'
]);
export default idGen;
You'll then have a provider with the following actions:
genFooId
- Increments and returns the currentfooId
.genBarId
- Increments and returns the currentbarId
.
And reducers:
newFooId
- The current newfooId
.newBarId
- The current newbarId
.
Real world example
See Lumbur's user login component.
Protip
On the server, you should probably set isGlobal
to true
. This will ensure the same provider instance is used across multiple requests and prevent any race conditions with replication if/when some ID of the same key is generated for more than one request at almost the exact same time.