From e10ba555ce4b74af6881d20276ab7f8f2d7a25e7 Mon Sep 17 00:00:00 2001 From: Robert Snow Date: Fri, 7 Mar 2025 11:13:35 +1100 Subject: [PATCH] fix: Environment with no FinalizationRegistry --- packages/@react-aria/utils/src/useId.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/@react-aria/utils/src/useId.ts b/packages/@react-aria/utils/src/useId.ts index 032488052d7..9238a602a3d 100644 --- a/packages/@react-aria/utils/src/useId.ts +++ b/packages/@react-aria/utils/src/useId.ts @@ -26,9 +26,12 @@ export let idsUpdaterMap: Map = new Map(); // This allows us to clean up the idsUpdaterMap when the id is no longer used. // Map is a strong reference, so unused ids wouldn't be cleaned up otherwise. // This can happen in suspended components where mount/unmount is not called. -let registry = new FinalizationRegistry((heldValue) => { - idsUpdaterMap.delete(heldValue); -}); +let registry; +if (typeof window !== 'undefined' && window.FinalizationRegistry) { + registry = new FinalizationRegistry((heldValue) => { + idsUpdaterMap.delete(heldValue); + }); +} /** * If a default is not provided, generate an id. @@ -41,7 +44,9 @@ export function useId(defaultId?: string): string { let res = useSSRSafeId(value); let cleanupRef = useRef(null); - registry.register(cleanupRef, res); + if (registry) { + registry.register(cleanupRef, res); + } if (canUseDOM) { const cacheIdRef = idsUpdaterMap.get(res); @@ -57,7 +62,9 @@ export function useId(defaultId?: string): string { return () => { // In Suspense, the cleanup function may be not called // when it is though, also remove it from the finalization registry. - registry.unregister(cleanupRef); + if (registry) { + registry.unregister(cleanupRef); + } idsUpdaterMap.delete(r); }; }, [res]);