Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate unique id in react native

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]

like image 978
RS Motors Avatar asked Dec 06 '25 15:12

RS Motors


1 Answers

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

like image 91
SKeney Avatar answered Dec 08 '25 03:12

SKeney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!