76dd10ea55
* add `prettier-plugin-organize-imports` and `prettier-plugin-tailwindcss` * format * bump Tailwind CSS * format playgrounds using updated Tailwind CSS and Prettier plugins * use import syntax
22 lines
478 B
TypeScript
22 lines
478 B
TypeScript
import { useEffect, useRef } 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
|
|
|
|
return () => {
|
|
trulyUnmounted.current = true
|
|
microTask(() => {
|
|
if (!trulyUnmounted.current) return
|
|
|
|
stableCb()
|
|
})
|
|
}
|
|
}, [stableCb])
|
|
}
|