Skip to content

Commit

Permalink
ensure cb in useOnUnmount is a stable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Apr 26, 2023
1 parent 5cfbb4b commit 74c9873
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/@headlessui-react/src/hooks/use-on-unmount.ts
@@ -1,7 +1,10 @@
import { useRef, useEffect } from 'react'
import { microTask } from '../utils/micro-task'
import { useEvent } from './use-event'

export function useOnUnmount(cb: () => void) {
let stableCb = useEvent(cb)

let trulyUnmounted = useRef(false)
useEffect(() => {
trulyUnmounted.current = false
Expand All @@ -11,8 +14,8 @@ export function useOnUnmount(cb: () => void) {
microTask(() => {
if (!trulyUnmounted.current) return

cb()
stableCb()
})
}
}, [])
}, [stableCb])
}

2 comments on commit 74c9873

@vercel
Copy link

@vercel vercel bot commented on 74c9873 Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-vue – ./packages/playground-vue

headlessui-vue-git-main-tailwindlabs.vercel.app
headlessui-vue-tailwindlabs.vercel.app
headlessui-vue.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 74c9873 Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-react – ./packages/playground-react

headlessui-react-tailwindlabs.vercel.app
headlessui-react-git-main-tailwindlabs.vercel.app
headlessui-react.vercel.app

Please sign in to comment.