Skip to content

Commit 74c9873

Browse files
committed
ensure cb in useOnUnmount is a stable reference
1 parent 5cfbb4b commit 74c9873

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { useRef, useEffect } from 'react'
22
import { microTask } from '../utils/micro-task'
3+
import { useEvent } from './use-event'
34

45
export function useOnUnmount(cb: () => void) {
6+
let stableCb = useEvent(cb)
7+
58
let trulyUnmounted = useRef(false)
69
useEffect(() => {
710
trulyUnmounted.current = false
@@ -11,8 +14,8 @@ export function useOnUnmount(cb: () => void) {
1114
microTask(() => {
1215
if (!trulyUnmounted.current) return
1316

14-
cb()
17+
stableCb()
1518
})
1619
}
17-
}, [])
20+
}, [stableCb])
1821
}

0 commit comments

Comments
 (0)