servatrice/webclient/src/hooks/useReduxEffect.tsx
Jeremy Letto 26d7fe2ff0
Webatrice: update deps (#4700)
* save work

* fix reset styling

* fix toast reducer

* update non-react deps

* update react libraries

* remove jquery, use sanitize-html instead

* add missing change

* fix deps and dev deps

* update workflow to target Node 16

* run @mui/codemod to remove @mui/styles

* add default body font size

* update react 17 to 18

* declare enum before use

* add rel attr to links

* fix font sizing issue

* trailing commas

* refactor deep destructuring

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
2022-11-01 12:41:42 -05:00

47 lines
1.1 KiB
TypeScript

/**
File is adapted from https://github.com/Qeepsake/use-redux-effect under MIT License
* @author Aspect Apps Limited
* @description
*/
import { useRef, useEffect, DependencyList } from 'react'
import { useStore } from 'react-redux'
import { AnyAction } from 'redux'
import { castArray } from 'lodash'
export type ReduxEffect = (action: AnyAction) => void
/**
* Subscribes to redux store events
*
* @param effect
* @param type
* @param deps
*/
export function useReduxEffect(
effect: ReduxEffect,
type: string | string[],
deps: DependencyList = [],
): void {
const currentValue = useRef(null);
const store = useStore();
const handleChange = (): void => {
const state: any = store.getState();
const action = state.action;
const previousValue = currentValue.current;
currentValue.current = action.count;
if (
previousValue !== action.count &&
castArray(type).includes(action.type)
) {
effect(action);
}
}
useEffect(() => {
const unsubscribe = store.subscribe(handleChange);
return (): void => unsubscribe();
}, deps)
}