What is react-native-uuid?
The react-native-uuid package is a utility for generating universally unique identifiers (UUIDs) in React Native applications. It supports various versions of UUIDs, including v1, v4, and v5, making it versatile for different use cases where unique identifiers are required.
What are react-native-uuid's main functionalities?
Generate UUID v1
Generates a UUID based on the current timestamp and the node ID (usually the MAC address). This is useful for generating unique IDs that are time-based.
const uuidv1 = require('react-native-uuid').v1;
const id = uuidv1();
console.log(id);
Generate UUID v4
Generates a random UUID. This is useful for creating unique identifiers that do not need to be predictable or based on any specific input.
const uuidv4 = require('react-native-uuid').v4;
const id = uuidv4();
console.log(id);
Generate UUID v5
Generates a UUID based on a namespace and a name. This is useful for generating unique IDs that are derived from specific inputs, ensuring consistency for the same input.
const uuidv5 = require('react-native-uuid').v5;
const MY_NAMESPACE = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
const id = uuidv5('hello.example.com', MY_NAMESPACE);
console.log(id);
Other packages similar to react-native-uuid
uuid
The uuid package is a popular library for generating UUIDs in JavaScript. It supports the same versions of UUIDs (v1, v4, and v5) and is widely used in both Node.js and browser environments. Compared to react-native-uuid, it is more general-purpose and not specifically tailored for React Native.
nanoid
Nanoid is a small, secure, URL-friendly unique string ID generator for JavaScript. It is much smaller in size compared to uuid and react-native-uuid, making it a good choice for performance-sensitive applications. However, it does not generate standard UUIDs, which might be a limitation for some use cases.
shortid
Shortid is a concise, non-sequential, URL-friendly unique ID generator. It is designed to be shorter than UUIDs and is useful for cases where a smaller ID is sufficient. However, it is not as widely adopted as uuid or react-native-uuid and does not follow the UUID standard.
react-native-uuid
react-native-uuid
is a zero-dependency TypeScript implementation of RFC4122 standard A Universally Unique IDentifier (UUID) URN Namespace. Please note, this library uses pseudo random generator based on top of Math.random
. New version with hardware support is WIP.
Heavily inspired by:
Huge thanks to Randy Coulman for the early version of a code.
Getting started
Use this steps to install and create UUIDs. Example project is available here
1. Install
npm install react-native-uuid
2. Create a UUID
import uuid from 'react-native-uuid';
uuid.v4();
Documentation
Methods documentation is available here
Troubleshooting
Previous version has been based on randombytes
that is not compatible with react-native out of the box.
Please submit an issue if you found a bug.
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
Thank you to our sponsors:
License
MIT
Copyright (c) 2016-2024 Eugene Hauptmann