use-unmount

PreviousNext

A hook to run a function when the component unmounts

Docs
hookshook

Preview

Loading preview…
registry/hooks/use-unmount.ts
import { useEffect } from 'react'
import { useLatest } from '@/registry/hooks/use-latest'

export function useUnmount(fn: () => void) {
  const fnRef = useLatest(fn)

  useEffect(
    () => () => {
      fnRef.current()
    },
    [],
  )
}

Installation

npx shadcn@latest add @hooks/use-unmount

Usage

import { UseUnmount } from "@/hooks/use-unmount"
const value = UseUnmount()