React Native UUID Generator

2.0.4 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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;

view raw JSON →