Skip to content

useUnmount

useUnmount 在组件卸载时执行的 Hook。

类型声明

ts
declare function 
useUnmount
(
fn
: () => void): void;

源码

ts
import { 
useEffect
} from 'react';
import
useLatest
from '../useLatest';
import {
isFunction
} from '../utils';
import
isDev
from '../utils/isDev';
const
useUnmount
= (
fn
: () => void) => {
if (
isDev
) {
if (!
isFunction
(
fn
)) {
console
.
error
(`useUnmount expected parameter is a function, got ${typeof
fn
}`);
} } const
fnRef
=
useLatest
(
fn
);
useEffect
(
() => () => {
fnRef
.
current
();
}, [], ); }; export default
useUnmount
;