{"id":15203,"library":"react-native-uuid","title":"React Native UUID Generator","description":"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.","status":"active","version":"2.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/eugenehp/react-native-uuid","tags":["javascript","uuid","rfc4122","react-native","reactivelions","typescript"],"install":[{"cmd":"npm install react-native-uuid","lang":"bash","label":"npm"},{"cmd":"yarn add react-native-uuid","lang":"bash","label":"yarn"},{"cmd":"pnpm add react-native-uuid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The default export is an object containing methods like `v1`, `v3`, `v4`, `v5`, `parse`, `stringify`, etc. Use `uuid.v4()` to generate a version 4 UUID. Since v2.0.1, default exports were explicitly fixed.","wrong":"import { uuid } from 'react-native-uuid';","symbol":"uuid (default export object)","correct":"import uuid from 'react-native-uuid';"},{"note":"Individual UUID version generation functions like `v4` are also available as named exports. This is often preferred for tree-shaking.","wrong":"import uuidV4 from 'react-native-uuid';","symbol":"v4 (named export)","correct":"import { v4 } from 'react-native-uuid';"},{"note":"For CommonJS environments, `require` returns the default export object. Access specific UUID generation methods via properties, e.g., `uuid.v4()`. While some libraries allow destructuring named exports from `require`, `react-native-uuid` primarily exposes its API via the default object for CJS.","wrong":"const { v4 } = require('react-native-uuid');","symbol":"CommonJS require","correct":"const uuid = require('react-native-uuid');"}],"quickstart":{"code":"import uuid, { v4, v1 } from 'react-native-uuid';\nimport React from 'react';\nimport { View, Text, Button, StyleSheet } from 'react-native';\n\nconst App = () => {\n  const [generatedUuids, setGeneratedUuids] = React.useState<string[]>([]);\n\n  const generateNewUuids = () => {\n    const newV4Uuid = v4() as string; // Using named export for v4\n    const newV1Uuid = uuid.v1() as string; // Using default export for v1\n    const anotherV4Uuid = uuid.v4() as string; // Using default export for v4\n    setGeneratedUuids(prev => [\n      `v4 (named): ${newV4Uuid}`,\n      `v1 (default): ${newV1Uuid}`,\n      `v4 (default): ${anotherV4Uuid}`,\n      ...prev.slice(0, 4) // Keep only the latest 5 UUIDs\n    ]);\n  };\n\n  return (\n    <View style={styles.container}>\n      <Text style={styles.title}>Generated UUIDs:</Text>\n      {\n        generatedUuids.length === 0 ?\n        <Text style={styles.noUuids}>Press the button to generate UUIDs.</Text> :\n        generatedUuids.map((id, index) => (\n          <Text key={index} style={styles.uuidText}>{id}</Text>\n        ))\n      }\n      <Button title=\"Generate New UUIDs\" onPress={generateNewUuids} />\n    </View>\n  );\n};\n\nconst styles = StyleSheet.create({\n  container: {\n    flex: 1,\n    justifyContent: 'center',\n    alignItems: 'center',\n    padding: 20,\n    backgroundColor: '#f0f0f0'\n  },\n  title: {\n    fontSize: 20,\n    fontWeight: 'bold',\n    marginBottom: 10\n  },\n  uuidText: {\n    fontSize: 14,\n    marginBottom: 5,\n    fontFamily: 'monospace'\n  },\n  noUuids: {\n    fontSize: 16,\n    color: '#888',\n    marginBottom: 20\n  }\n});\n\nexport default App;\n","lang":"typescript","description":"Demonstrates generating different UUID versions (v4 and v1) using both default and named imports within a React Native functional component, showing how to update state with new UUIDs."},"warnings":[{"fix":"Review migration guides or package documentation when upgrading from 1.x. Specific fixes for default exports were provided in v2.0.1, so ensure your import statements are aligned with the new API surface.","message":"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.","severity":"breaking","affected_versions":">=2.0.0 <2.0.1"},{"fix":"Ensure your import statements correctly handle the default export, which is an object containing UUID generation methods (e.g., `import uuid from 'react-native-uuid'; uuid.v4();`). Review `ARCHITECTURE.md` for correct import patterns.","message":"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.","severity":"breaking","affected_versions":">=2.0.0 <2.0.1"},{"fix":"Upgrade to `react-native-uuid` version 2.0.4 or newer. For environments where `crypto.getRandomValues()` is still unsupported (e.g., specific React Native versions or debugging with Chrome), consider installing a polyfill like `react-native-get-random-values` and importing it at your app's entry point (`import 'react-native-get-random-values';`).","message":"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.","severity":"gotcha","affected_versions":"<2.0.4"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Use `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();`.","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.","error":"TypeError: uuid.v4 is not a function"},{"fix":"Ensure 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.","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.","error":"Error: crypto.getRandomValues() not supported"},{"fix":"Run `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`).","cause":"The package has not been correctly installed or dependencies are not linked/resolved in your project.","error":"Cannot find module 'react-native-uuid'"}],"ecosystem":"npm"}