From 2b836560d6c35601b9c9f2507e377975cfac4d07 Mon Sep 17 00:00:00 2001 From: Valerii Sidorenko Date: Thu, 10 Aug 2023 12:47:30 +0200 Subject: [PATCH] feat: use useId from React 18 to generate uniq ids (#883) --- src/components/utils/useUniqId.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/utils/useUniqId.ts b/src/components/utils/useUniqId.ts index a9bb50ba4f..cf660620f3 100644 --- a/src/components/utils/useUniqId.ts +++ b/src/components/utils/useUniqId.ts @@ -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(); + 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;