1.0.2 • Published 3 years ago

@byteclaw/use-unique-id v1.0.2

Weekly downloads
339
License
MIT
Repository
github
Last release
3 years ago

@Byteclaw/use-unique-id

React hook to generate unique ids for your components.

Installation

npm install @byteclaw/use-unique-id
yarn add @byteclaw/use-unique-id

Usage

Client side only

import { useUniqueId } from '@byteclaw/use-unique-id';
import React, { useCallback } from 'react';

function Element() {
  const id = useUniqueId();
}

function App() {
  return <Element />;
}

Server side support

In order to have consistent unique ids across client and server side render please provider custom UniqueIdProvider.

import { UniqueIdProvider, useUniqueId } from '@byteclaw/use-unique-id';
import React, { useCallback } from 'react';

function Element() {
  const id = useUniqueId();
}

function App() {
  return (
    <UniqueIdProvider>
      <Element />
    </UniqueIdProvider>
  );
}