致谢: Re-export from react-use
一个生命周期 Hook,提供一个高阶 Promise,如果组件卸载则该 Promise 不会 resolve。
import { useUnmountPromise } from "@lynx-js/react-use"; const Demo = () => { const mounted = useUnmountPromise(); useEffect(async () => { await mounted(someFunction()); // Will not resolve if component un-mounts. }); };
type Race = <P extends Promise<any>, E = any>( promise: P, onError?: (error: E) => void ) => P; declare const useUnmountPromise: () => Race;