useWillUnmount.js 412 Bytes
Newer Older
Sangjune Bae's avatar
Sangjune Bae committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import useUpdatedRef from './useUpdatedRef';
import { useEffect } from 'react';
/**
 * Attach a callback that fires when a component unmounts
 *
 * @param fn Handler to run when the component unmounts
 * @category effects
 */

export default function useWillUnmount(fn) {
  var onUnmount = useUpdatedRef(fn);
  useEffect(function () {
    return function () {
      return onUnmount.current();
    };
  }, []);
}