mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
15 lines
378 B
TypeScript
15 lines
378 B
TypeScript
import { useCallback } from 'react';
|
|
import { useMountedState } from 'react-use';
|
|
|
|
export const useSafeReducerDispatch = <A>(dispatch: (action: A) => void) => {
|
|
const isMounted = useMountedState();
|
|
|
|
const safeDispatch = useCallback<typeof dispatch>((...args) => {
|
|
if (isMounted()) {
|
|
dispatch(...args);
|
|
}
|
|
}, [dispatch, isMounted]);
|
|
|
|
return safeDispatch;
|
|
};
|