grafana/public/app/core/hooks/useCleanup.ts
kay delaney 64bbb7a7ce
Chore: Update and enforce usage of typed react-redux hooks (#55349)
* Chore: Update and enforce usage of typed react-redux hooks
2022-09-19 10:49:35 +01:00

18 lines
602 B
TypeScript

import { useEffect, useRef } from 'react';
import { useDispatch } from 'app/types';
import { cleanUpAction, CleanUpAction } from '../actions/cleanUp';
export function useCleanup(cleanupAction: CleanUpAction) {
const dispatch = useDispatch();
//bit of a hack to unburden user from having to wrap stateSelcetor in a useCallback. Otherwise cleanup would happen on every render
const selectorRef = useRef(cleanupAction);
selectorRef.current = cleanupAction;
useEffect(() => {
return () => {
dispatch(cleanUpAction({ cleanupAction: selectorRef.current }));
};
}, [dispatch]);
}