mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
16 lines
608 B
TypeScript
16 lines
608 B
TypeScript
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]);
|
|
}
|