Skip to content

Commit

Permalink
fix: Environment with no FinalizationRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
snowystinger committed Mar 7, 2025
1 parent b4695c8 commit e10ba55
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/@react-aria/utils/src/useId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export let idsUpdaterMap: Map<string, { current: string | null }[]> = 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<string>((heldValue) => {
idsUpdaterMap.delete(heldValue);
});
let registry;
if (typeof window !== 'undefined' && window.FinalizationRegistry) {
registry = new FinalizationRegistry<string>((heldValue) => {
idsUpdaterMap.delete(heldValue);
});
}

/**
* If a default is not provided, generate an id.
Expand All @@ -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);
Expand All @@ -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]);
Expand Down

0 comments on commit e10ba55

Please sign in to comment.