import { useCallback } from 'react'; import { useMountedState } from 'react-use'; export const useSafeReducerDispatch = (dispatch: (action: A) => void) => { const isMounted = useMountedState(); const safeDispatch = useCallback((...args) => { if (isMounted()) { dispatch(...args); } }, [dispatch, isMounted]); return safeDispatch; };