react-generate-unique-key-for-map
Version:
Create unique keys for map elements that don't have unique property
19 lines (14 loc) • 482 B
text/typescript
import { useRef } from "react";
import { generateUniqueKey } from "./generateUniqueKey";
export function useGetUniqueKey() {
const uniqueKeysRef = useRef(new Map<unknown, string>());
function getUniqueKey<T>(item: T): string {
if (uniqueKeysRef.current.has(item)) {
return uniqueKeysRef.current.get(item) as string;
}
const uniqueKey = generateUniqueKey();
uniqueKeysRef.current.set(item, uniqueKey);
return uniqueKey;
}
return getUniqueKey;
}