Skip to content

Commit

Permalink
feat: use useId from React 18 to generate uniq ids (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Aug 10, 2023
1 parent 7c54a5f commit 2b83656
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/components/utils/useUniqId.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import React from 'react';

import {NAMESPACE_NEW} from './cn';
import {getUniqId} from './common';

export function useUniqId() {
const idRef = React.useRef(getUniqId());
function useUniqIdFallback() {
const idRef = React.useRef<string>();
if (idRef.current === undefined) {
idRef.current = getUniqId();
}
return idRef.current;
}

function useIdNative() {
return `${NAMESPACE_NEW}${React.useId()}`;
}

export const useUniqId: () => string =
typeof React.useId === 'function' ? useIdNative : useUniqIdFallback;

0 comments on commit 2b83656

Please sign in to comment.