uuid npm typescript code example

Example 1: uuid react

import React from 'react'
import uuid from 'react-uuid'

const array = ['one', 'two', 'three']

export const LineItem = item => <li key={uuid()}>{item}</li>

export const List = () => array.map(item => <LineItem item={item} />)

Example 2: uuid react

npm i react-uuid

Example 3: create uuid typescript

// Method 1: use 'uuid' NPM library
//  $ npm i uuid

import { v4 as uuid } from 'uuid';
// ----------------or----------------
const uuid = require('uuid/v4');

const myUUID = uuid();

// Method 2: manually write uuid function
const uuid = () =>
  "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    var r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });

const myUUID = uuid();

/*
	Hope this answered your question. Happy coding :)
*/