Skip to content

Commit 076b03c

Browse files
Skip updating state if sentinel has been unmounted (#2585)
* Skip updating state if sentinel has been unmounted * use existing `useIsMounted` hook * reformat code paths --------- Co-authored-by: Robin Malfait <[email protected]>
1 parent 00fdb7e commit 076b03c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

packages/@headlessui-react/src/internal/focus-sentinel.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState, FocusEvent as ReactFocusEvent } from 'react'
22

3+
import { useIsMounted } from '../hooks/use-is-mounted'
34
import { Hidden, Features } from './hidden'
45

56
interface FocusSentinelProps {
@@ -8,6 +9,7 @@ interface FocusSentinelProps {
89

910
export function FocusSentinel({ onFocus }: FocusSentinelProps) {
1011
let [enabled, setEnabled] = useState(true)
12+
let mounted = useIsMounted()
1113

1214
if (!enabled) return null
1315

@@ -31,8 +33,10 @@ export function FocusSentinel({ onFocus }: FocusSentinelProps) {
3133
// Try to move focus to the correct element. This depends on the implementation
3234
// of `onFocus` of course since it would be different for each place we use it in.
3335
if (onFocus()) {
34-
setEnabled(false)
3536
cancelAnimationFrame(frame)
37+
if (!mounted.current) return
38+
39+
setEnabled(false)
3640
return
3741
}
3842

0 commit comments

Comments
 (0)