Skip to content

Commit e10ba55

Browse files
committed
fix: Environment with no FinalizationRegistry
1 parent b4695c8 commit e10ba55

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/@react-aria/utils/src/useId.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ export let idsUpdaterMap: Map<string, { current: string | null }[]> = new Map();
2626
// This allows us to clean up the idsUpdaterMap when the id is no longer used.
2727
// Map is a strong reference, so unused ids wouldn't be cleaned up otherwise.
2828
// This can happen in suspended components where mount/unmount is not called.
29-
let registry = new FinalizationRegistry<string>((heldValue) => {
30-
idsUpdaterMap.delete(heldValue);
31-
});
29+
let registry;
30+
if (typeof window !== 'undefined' && window.FinalizationRegistry) {
31+
registry = new FinalizationRegistry<string>((heldValue) => {
32+
idsUpdaterMap.delete(heldValue);
33+
});
34+
}
3235

3336
/**
3437
* If a default is not provided, generate an id.
@@ -41,7 +44,9 @@ export function useId(defaultId?: string): string {
4144
let res = useSSRSafeId(value);
4245
let cleanupRef = useRef(null);
4346

44-
registry.register(cleanupRef, res);
47+
if (registry) {
48+
registry.register(cleanupRef, res);
49+
}
4550

4651
if (canUseDOM) {
4752
const cacheIdRef = idsUpdaterMap.get(res);
@@ -57,7 +62,9 @@ export function useId(defaultId?: string): string {
5762
return () => {
5863
// In Suspense, the cleanup function may be not called
5964
// when it is though, also remove it from the finalization registry.
60-
registry.unregister(cleanupRef);
65+
if (registry) {
66+
registry.unregister(cleanupRef);
67+
}
6168
idsUpdaterMap.delete(r);
6269
};
6370
}, [res]);

0 commit comments

Comments
 (0)