react-elegant-ui
Version:
Elegant UI components, made by BEM best practices for react
28 lines • 656 B
JavaScript
import { createContext, useContext, useRef } from 'react';
export var initialContextValue = {
id: 'default',
counter: 0
};
/**
* Property `counter` will be mutable
*/
export var UIDContext = /*#__PURE__*/createContext(initialContextValue);
/**
* Generate unique id
*
* You can use context `UIDContext` to set generator state
*
* @example
* const id = useUniqId()
*/
export function useUniqueId(prefix) {
if (prefix === void 0) {
prefix = 'uid';
}
var context = useContext(UIDContext);
var id = useRef(null);
if (id.current === null) {
id.current = [prefix, context.id, ++context.counter].join('-');
}
return id.current;
}