npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-uuid

v2.0.1

Published

A React Hook for generating a unique id for the lifetime of a Component

Downloads

1,875

Readme

react-use-uuid

When building a Component using React Hooks, there are instances where a unique id is necessary. With React class components one could simply generate an ID and store it as a property on the class instances (not state, though there is nothing wrong with using state). An unfortunate side effect of Hooks is that they aren't class instances and we must rely on a new paradigm for generating a unique id for the lifetime of our component.

useId does just that by creating a memoized id generator. The benefit is two-fold. First, the id generator only runs on the first render, saving us from wasting time generating ids that won't be used. Second, our id never changes for the entire lifecycle of our component, giving us a bit more reliability when using said id.

How to use it

import React, { useCallback, useEffect } from 'react';
import useId from 'react-use-uuid';

function MyComponent() {
    const id = useId();
    const onDrop = useCallback((files) => console.log(`Received ${files.length} files`));

    useEffect(() => {
        // Third party code with limited integration
        Aspera.Connect.setDragDropTargets(`#${id}`, onDrop);
    }, []);

    return(
        <div id={id}>
            Drop your files here
        </div>
    );
}

Use a different version of UUID ID generation

Behind the scenes, useId makes use of the powerful uuid package. By default, version 4 of the id generation is used but all versions are supported through an optional parameter to useId.

import React, { useCallback, useEffect } from 'react';
import useId from 'react-use-uuid';
import uuidv5 from 'uuid/v5';

function MyComponent() {
    const id = useId('v5', 'hello.example.com', uuidv5.DNS);
    const onDrop = useCallback((files) => console.log(`Received ${files.length} files`));

    useEffect(() => {
        // Third party code with limited integration
        Aspera.Connect.setDragDropTargets(`#${id}`, onDrop);
    }, []);

    return(
        <div id={id}>
            Drop your files here
        </div>
    );
}

Custom id hook

If you find your application needs v3 or v5 of uuid, which require additional properties to generate an id, create a custom hook to keep your code readable and DRY.

// UseCustomId.js
import useId from 'react-use-uuid';
import uuidv5 from 'uuid/v5';

function useCustomId() {
    return useId('v5', 'hello.example.com', uuidv5.DNS);
}
// MyComponent.js
import React, { useCallback, useEffect } from 'react';
import useCustomId from './useCustomId';

function MyComponent() {
    const id = useCustomId();
    const onDrop = useCallback((files) => console.log(`Received ${files.length} files`));

    useEffect(() => {
        // Third party code with limited integration
        Aspera.Connect.setDragDropTargets(`#${id}`, onDrop);
    }, []);

    return(
        <div id={id}>
            Drop your files here
        </div>
    );
}