1

Can anyone suggest how I'd go about creating an ID-generating function in React?

So far I've tried to get this to work through an uuid npm package https://www.npmjs.com/package/uuid,

import uuidv4 from 'uuid'

    helpIdGenerator {
        const uuidv4 = require('uuid/v4')
    }

But it doesn't seem to work.

halfer
  • 19,824
  • 17
  • 99
  • 186
Valeri Lagunov
  • 89
  • 1
  • 1
  • 7

3 Answers3

13

This function may be useful:

function guidGenerator() {
    var S4 = function() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    };
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

this generates random ids like this:

6b33fce8-1745-f8de-4ad8-4ee42585oprf

Riccardo Persiani
  • 702
  • 1
  • 5
  • 25
2

This works for me:

import uuid from 'react-native-uuid'

let id = uuid.v1()
pglez82
  • 489
  • 5
  • 11
1

(Posted solution on behalf of the question author).

Fixed by using:

helpIdGenerator = () => {

    const uuidv4 = require('uuid/v4')

}
halfer
  • 19,824
  • 17
  • 99
  • 186