mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 13:43:39 +01:00
17 lines
514 B
TypeScript
17 lines
514 B
TypeScript
import { useState } from 'react';
|
|
|
|
export function useUniqueKey(len = 10): { key: string; regenerate: () => void } {
|
|
const [key, setKey] = useState<string>(() => generate(len));
|
|
return { key, regenerate: () => setKey(generate(len)) };
|
|
}
|
|
|
|
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
|
|
function generate(len: number): string {
|
|
const chars = [];
|
|
for (let i = 0; i < len; i++) {
|
|
chars.push(CHARS[Math.floor(Math.random() * CHARS.length)]);
|
|
}
|
|
return chars.join('');
|
|
}
|