2021-04-16 03:08:26 -05:00
|
|
|
import { useEffect, useRef } from 'react';
|
2022-09-19 04:49:35 -05:00
|
|
|
|
|
|
|
import { useDispatch } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-09-05 07:56:08 -05:00
|
|
|
import { cleanUpAction, CleanUpAction } from '../actions/cleanUp';
|
2021-04-16 03:08:26 -05:00
|
|
|
|
2022-09-05 07:56:08 -05:00
|
|
|
export function useCleanup(cleanupAction: CleanUpAction) {
|
2021-04-16 03:08:26 -05:00
|
|
|
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
|
2022-09-05 07:56:08 -05:00
|
|
|
const selectorRef = useRef(cleanupAction);
|
|
|
|
selectorRef.current = cleanupAction;
|
2021-04-16 03:08:26 -05:00
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
2022-09-05 07:56:08 -05:00
|
|
|
dispatch(cleanUpAction({ cleanupAction: selectorRef.current }));
|
2021-04-16 03:08:26 -05:00
|
|
|
};
|
|
|
|
}, [dispatch]);
|
|
|
|
}
|