React Native UUID Generator
react-native-uuid is a robust, zero-dependency TypeScript implementation of the RFC4122 standard for generating Universally Unique Identifiers (UUIDs). It is specifically designed and optimized for React Native environments, ensuring compatibility across various JavaScript runtimes, including Node.js and browsers. The library is currently stable at version 2.0.4, with a development focus on performance, security, and standards compliance. A significant rewrite in version 2.0.0 introduced a pure TypeScript codebase and eliminated external dependencies. Recent updates, such as v2.0.4, have focused on improving the random number generation (RNG) by ensuring compatibility with `crypto.getRandomValues()` API, enhancing security and randomness quality. The package includes comprehensive test suites and continuous integration benchmarks for performance and security metrics, distinguishing it from libraries that might rely on less secure `Math.random` implementations for entropy.
Common errors
-
TypeError: uuid.v4 is not a function
cause Incorrect import: attempting to destructure `uuid` as a named export when it's the name of the default export object, or calling the default import as a function directly.fixUse `import uuid from 'react-native-uuid';` and then call `uuid.v4();` OR use the named import `import { v4 } from 'react-native-uuid';` and then call `v4();`. -
Error: crypto.getRandomValues() not supported
cause The JavaScript runtime (e.g., React Native with Hermes, or older browser environments) does not natively provide the `crypto.getRandomValues()` API, which `react-native-uuid` (especially since v2.0.4) attempts to use for stronger randomness.fixEnsure you are on `react-native-uuid` version 2.0.4 or newer. If the error persists, install `react-native-get-random-values` (`npm install react-native-get-random-values`) and add `import 'react-native-get-random-values';` at the very top of your application's entry file (e.g., `index.js` or `App.js`) to polyfill the API. -
Cannot find module 'react-native-uuid'
cause The package has not been correctly installed or dependencies are not linked/resolved in your project.fixRun `npm install react-native-uuid` or `yarn add react-native-uuid`. If using React Native, ensure your caches are clear (`npx react-native start --reset-cache`) and rebuild your project (`npx react-native run-ios` / `npx react-native run-android`).
Warnings
- breaking Version 2.0.0 introduced a complete rewrite of react-native-uuid, transitioning to a zero-dependency TypeScript codebase. This major version change likely broke API compatibility with version 1.x, requiring code adjustments for migration.
- breaking Following the v2.0.0 rewrite, version 2.0.1 specifically addressed and fixed issues related to default exports. Code relying on initial v2.0.0 default export behavior might have broken when upgrading to 2.0.1.
- gotcha Prior to version 2.0.4, `react-native-uuid` primarily relied on `Math.random` for its pseudo-random number generation. While v2.0.4 fixed RNG to use `Uint8Array` for `crypto.getRandomValues()` API compatibility, older React Native environments (especially with Hermes) or older versions of this library might still default to `Math.random` if `crypto.getRandomValues` is unavailable or not polyfilled. `Math.random` is generally not considered cryptographically secure.
Install
-
npm install react-native-uuid -
yarn add react-native-uuid -
pnpm add react-native-uuid
Imports
- uuid (default export object)
import { uuid } from 'react-native-uuid';import uuid from 'react-native-uuid';
- v4 (named export)
import uuidV4 from 'react-native-uuid';
import { v4 } from 'react-native-uuid'; - CommonJS require
const { v4 } = require('react-native-uuid');const uuid = require('react-native-uuid');
Quickstart
import uuid, { v4, v1 } from 'react-native-uuid';
import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
const App = () => {
const [generatedUuids, setGeneratedUuids] = React.useState<string[]>([]);
const generateNewUuids = () => {
const newV4Uuid = v4() as string; // Using named export for v4
const newV1Uuid = uuid.v1() as string; // Using default export for v1
const anotherV4Uuid = uuid.v4() as string; // Using default export for v4
setGeneratedUuids(prev => [
`v4 (named): ${newV4Uuid}`,
`v1 (default): ${newV1Uuid}`,
`v4 (default): ${anotherV4Uuid}`,
...prev.slice(0, 4) // Keep only the latest 5 UUIDs
]);
};
return (
<View style={styles.container}>
<Text style={styles.title}>Generated UUIDs:</Text>
{
generatedUuids.length === 0 ?
<Text style={styles.noUuids}>Press the button to generate UUIDs.</Text> :
generatedUuids.map((id, index) => (
<Text key={index} style={styles.uuidText}>{id}</Text>
))
}
<Button title="Generate New UUIDs" onPress={generateNewUuids} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: '#f0f0f0'
},
title: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 10
},
uuidText: {
fontSize: 14,
marginBottom: 5,
fontFamily: 'monospace'
},
noUuids: {
fontSize: 16,
color: '#888',
marginBottom: 20
}
});
export default App;