16

I am new to react native and working on my first project in expo. I am trying to generate unique id for every order that is being placed by the user, below is what I have tried

 const orderId = () => {
    var S4 = () => {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (
      S4() +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      S4() +
      S4()
    );
  };
  console.log(orderId);

what I am getting in terminal is [Function orderId]

Kelton Temby
  • 825
  • 10
  • 20
RS Motors
  • 215
  • 1
  • 4
  • 8
  • if you can use libraries, try this one -> https://www.npmjs.com/package/react-native-uuid totally helpful, easy to use and light – poPaTheGuru May 25 '21 at 06:02
  • 1
    Hey thanks for responding .. tried it and it worked. thanks! – RS Motors May 25 '21 at 06:09
  • 1
    Please check this https://jsbin.com/kiyunabesu/edit?js,console – Manoj Bhardwaj May 25 '21 at 06:28
  • The real problem you had was calling the function reference `orderID` rather than executing the function `orderID()`. You can simply change the last line to execute the function and your algorithm will generate UUID's. – Kelton Temby Mar 01 '23 at 10:22

6 Answers6

24

You can use the following:

https://www.npmjs.com/package/react-native-uuid

npm install react-native-uuid

import uuid from 'react-native-uuid';
uuid.v4(); // ⇨ '11edc52b-2918-4d71-9058-f7285e29d894'

If you are not able to use other libraries in your project see the following related question:
How to create a GUID / UUID

SKeney
  • 1,965
  • 2
  • 8
  • 30
2
function guidGenerator(){
    vary S4 = function(){
        return (((1+Math.random())*0x10000)|0).to String(16).substring(1);
    };
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
2

Ran into this problem today....tried to use nanoid in React Native and Baaam...
Well then...So why not write our own simple solution? This one does the job just fine :)

export function generateUUID(digits) {
    let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXZ';
    let uuid = [];
    for (let i = 0; i < digits; i++) {
        uuid.push(str[Math.floor(Math.random() * str.length)]);
    }
    return uuid.join('');
}

generateUUID(10) // 7ABLSma6F6
generateUUID(32) // FCGQ91Q5r2y3PkkPFuHhFh9JusMMDFwR

Felipe Chernicharo
  • 3,619
  • 2
  • 24
  • 32
0

Answering your code question rather than suggesting an alternative library -

The only problem you had was logging the function object reference orderID rather than executing the function orderID(). This is why the console output was [Function orderId].

You can simply change the last line to execute the function and your algorithm will generate UUID's.

const orderId = () => {

  const S4 = () => {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  };

  return (
    S4() +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    S4() +
    S4()
  );
}

console.log(orderId());

// output: "87dae617-994c-c506-3a43-da55ec0586e4"
Kelton Temby
  • 825
  • 10
  • 20
0

2023-02: Expo SDK 48 update

You can now use expo-crypto to generate uuid v4:

import * as Crypto from "expo-crypto";

const UUID = Crypto.randomUUID();

More info in the official documentation.

devpolo
  • 2,487
  • 3
  • 12
  • 28
-1

You can use urid package npm install urid

import urid from 'urid';

urid(); // qRpky22nKJ4vkbFZ

Read full docs here: https://www.npmjs.com/package/urid