Skip to content

Commit f2e6c68

Browse files
chaancelauri865
andcommitted
Fix Presence component memory leak (#3234)
Co-authored-by: Lauri <[email protected]>
1 parent 0fe8656 commit f2e6c68

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

.changeset/yellow-geese-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@radix-ui/react-presence': patch
3+
---
4+
5+
Fix memory leak in Presence

packages/react/presence/src/presence.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Presence.displayName = 'Presence';
3131

3232
function usePresence(present: boolean) {
3333
const [node, setNode] = React.useState<HTMLElement>();
34-
const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);
34+
const stylesRef = React.useRef<CSSStyleDeclaration | null>(null);
3535
const prevPresentRef = React.useRef(present);
3636
const prevAnimationNameRef = React.useRef<string>('none');
3737
const initialState = present ? 'mounted' : 'unmounted';
@@ -153,15 +153,15 @@ function usePresence(present: boolean) {
153153
return {
154154
isPresent: ['mounted', 'unmountSuspended'].includes(state),
155155
ref: React.useCallback((node: HTMLElement) => {
156-
if (node) stylesRef.current = getComputedStyle(node);
156+
stylesRef.current = node ? getComputedStyle(node) : null;
157157
setNode(node);
158158
}, []),
159159
};
160160
}
161161

162162
/* -----------------------------------------------------------------------------------------------*/
163163

164-
function getAnimationName(styles?: CSSStyleDeclaration) {
164+
function getAnimationName(styles: CSSStyleDeclaration | null) {
165165
return styles?.animationName || 'none';
166166
}
167167

0 commit comments

Comments
 (0)