useMountState
FunctionalComponent<>のmountの状態を取得するcustom Hooks
使用場面
非同期処理を行うevent handler
かつ、非同期処理の実行中にcomponentがunmountされる可能性があるもの
code
useRef()を使う
code:useMountState.ts
import * as React from 'react';
export const useMountState = () => {
const mounted = React.useRef(true);
// unmountされたことを検知する
React.useEffect(() => {
const cleanup = () => {
mounted.current = false;
};
return cleanup;
}, []);
return mounted.current;
};
References
CodeSandboxでFirebase authendicationを使ってみる#5fc0f5171280f00000df2ce4
react-firebase-hooksを使ってみた(Auth Hooks編) - Qiita
#2020-11-27 21:54:13