Files
grafana/public/app/core/hooks/useCleanup.ts
Hugo Häggmark 20c700dd52 Chore: reduces barrel files part II (#107688)
* Chore: reduce barrel files

* chore: fixes unit test

* Chore: reduces barrel files part II

* chore: fix import sorting
2025-07-09 06:15:33 +02:00

18 lines
608 B
TypeScript

import { useEffect, useRef } from 'react';
import { useDispatch } from 'app/types/store';
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]);
}