grafana/public/app/core/hooks/useCleanup.ts

17 lines
609 B
TypeScript
Raw Normal View History

import { useEffect, useRef } from 'react';
import { useDispatch } from 'react-redux';
import { cleanUpAction, StateSelector } from '../actions/cleanUp';
export function useCleanup<T>(stateSelector: StateSelector<T>) {
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(stateSelector);
selectorRef.current = stateSelector;
useEffect(() => {
return () => {
dispatch(cleanUpAction({ stateSelector: selectorRef.current }));
};
}, [dispatch]);
}