forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseId.ts
20 lines (19 loc) · 830 Bytes
/
useId.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import * as React from 'react';
import { getId } from '@uifabric/utilities/lib/getId';
/**
* Hook to generate a unique ID in the global scope (spanning across duplicate copies of the same library).
*
* @param prefix - Optional prefix for the ID
* @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,
* without conditioning the hook call
* @returns The ID
*/
export function useId(prefix?: string, providedId?: string): string {
// getId should only be called once since it updates the global constant for the next ID value.
// (While an extra update isn't likely to cause problems in practice, it's better to avoid it.)
const ref = React.useRef<string | undefined>(providedId);
if (!ref.current) {
ref.current = getId(prefix);
}
return ref.current;
}