@patreon/studio
Version:
Patreon Studio Design System
26 lines • 698 B
JavaScript
import { useEffect, useState } from 'react';
let idMap = {};
/**
* Given a prefix `name`, returns `name-id` where `id` is a number sequentially
* incremented for each component instance, starting with `0`.
*
* @example
* const id = useSequentialId('MyComponent') // returns `MyComponent-0`
*/
export function useSequentialId(prefix) {
const [id, setId] = useState();
useEffect(() => {
if (prefix in idMap) {
idMap[prefix] += 1;
}
else {
idMap[prefix] = 0;
}
setId(`${prefix}-${idMap[prefix]}`);
}, [prefix]);
return id;
}
export function clearSequentialIds() {
idMap = {};
}
//# sourceMappingURL=index.js.map