uuid

It is an algorithm used to generate unique identifiers. This algorithm ensures that all UUIDs generated in a network are globally unique, meaning they are not duplicated regardless of time or space.

Features

  1. Uniqueness: UUIDs can be generated uniquely anywhere in the world. This is essential for uniquely identifying objects in databases, files, clouds, and more.

  2. Non-traceability: UUIDs are randomly generated, making it impossible to trace back to the creator, time, or location solely based on the generated UUID. This characteristic plays an important role in areas such as privacy protection.

  3. Scalability: UUIDs are an international standard and have a scalable length, allowing for the generation of sufficient unique identifiers even in the future.


Installation

The command npm i uuid is used to install the package called uuid, which is used for generating Universally Unique Identifiers (UUIDs) in Node.js environments.

Installation

npm install uuid

Usage

The 'uuid' library is used to generate unique identifiers, which can be utilized for tasks such as tracking requests in web servers or uniquely identifying database records.

  • Name
    uuid import
    Type
    Usage
    Description

    You can generate UUIDs by invoking the v1 or v4 functions.

Cautions

  • Name
    Preventing Duplication
    Type
    Cautions
    Description

    UUIDs generate unique values with each invocation, so it is important to avoid using the same UUID multiple times within the same project.

  • Name
    Unpredictability
    Type
    Cautions
    Description

    UUIDs are randomly generated, making them unpredictable. Therefore, it is not recommended to directly use UUIDs as encryption keys or passwords.

  • Name
    Rendering Considerations
    Type
    Cautions
    Description

    When using UUIDs as keys for components in frameworks like React, be cautious as a new value will be generated with each render. This can lead to unexpected rendering issues.

  • Name
    Potential Collisions
    Type
    Cautions
    Description

    Theoretically, UUIDs are unique, but v4 (random) UUIDs have a small possibility of collision. While the actual probability is extremely low, it cannot be completely ruled out. Therefore, in specific use cases, relying solely on UUIDs for uniqueness may not be sufficient, and additional uniqueness checks may be necessary.

Usage

const { v1: uuidv1, v4: uuidv4 } = require('uuid');

console.log(uuidv1()); // '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
console.log(uuidv4()); // '16fd2706-8baf-433b-82eb-8c7fada847da'


Customization

The 'uuid' library provides several versions of UUID generation methods. Among them, the most commonly used versions are v1 and v4. Each version generates UUIDs in a unique way, which can be seen as a form of "customization" feature.

설명

  • Name
    v1
    Type
    Customization
    Description

    UUIDs generated by this version are based on timestamps and the MAC address of the machine. As a result, the generated UUIDs can vary depending on the time of creation and the machine generating them. From a customization perspective, this can be useful in distributed systems where UUIDs are generated concurrently across multiple machines. However, it should be noted that including the MAC address may raise privacy concerns.

  • Name
    v4
    Type
    Customization
    Description

    UUIDs generated by this version are purely random. While it is not possible for users to set the values of each digit, the randomness of the generated UUIDs can be seen as a form of customization. Random UUIDs have an equal probability of generating the same value regardless of the machine generating them, making them useful in systems where UUIDs are generated only within the same machine.

Customization

import { parse as uuidParse } from 'uuid';

// Parse a UUID
const bytes = uuidParse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b');

// Convert to hex strings to show byte order (for documentation purposes)
[...bytes].map((v) => v.toString(16).padStart(2, '0')); // ⇨
// [
// '6e', 'c0', 'bd', '7f',
// '11', 'c0', '43', 'da',
// '97', '5e', '2a', '8a',
// 'd9', 'eb', 'ae', '0b'
// ]

Example

To ensure unique keys, we are using uuidv4() here to generate a unique key for each element. uuidv4() guarantees uniqueness, making it suitable for generating unique keys for each element.

Additional Explanation

  • Name
    re-render
    Type
    Example
    Description

    Since uuidv4() is called within the map function, a new key is generated for each element whenever the component re-renders. This can lead to unnecessary re-renders in React, negatively impacting performance and causing issues with state persistence.

  • Name
    Solution
    Type
    Example
    Description

    If each item in rewards already has a unique ID, it is recommended to use that ID as the key. Otherwise, it is preferable to assign a uuid to each item in the rewards data when it is initially created or fetched, and use that uuid as the key. This approach avoids the issue of generating a new key with each component re-render.

/lostark/calendar.jsx

import React from 'react'

import { v4 as uuidv4 } from 'uuid'

export default function Reward({ rewards }) {
    return (
        <div className="p-6">
            <h2 className="mb-4 text-2xl font-bold">보상</h2>
            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3">
                {rewards.map((reward) => (
                <div className="rounded-lg border p-4" key={uuidv4()}>
                    <h3 className="text-xl font-semibold">{reward.name}</h3>
                    <span className="text-sm text-gray-500">{reward.type}</span>
                    <ul className="mt-2 list-inside list-disc">
                    {reward.items.map((item) => (
                        <li key={uuidv4()}>{item}</li>
                    ))}
                    </ul>
                </div>
                ))}
            </div>
        </div>
    )
}