2021-04-16 03:08:26 -05:00
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
import { useDispatch } from 'react-redux';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2021-04-16 03:08:26 -05:00
|
|
|
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]);
|
|
|
|
}
|