moi
Published on

Generate Random Key

Authors
  • avatar
    Name
    Mathieu Sévégny
    Twitter

Generating a Random Key

Why?

Sometimes in React, you have a list of components. If you haven't already added a key to each component, it will show an error in the console. This function creates an unique key.

It is not a good practice but sometimes it simplify your code. 🤷‍♂️

Installation

You need the uuid NPM package.

Code Block

import {v4 as uuidv4} from 'uuid';

function createRandomKey(){
    return uuidv4();
}