import { useCallback, useMemo } from 'preact/compat'; import { useDispatch } from 'react-redux'; import { BoundActionCreator, BoundActionCreators } from '@app/utils/actionBinder'; /* eslint-disable @typescript-eslint/no-explicit-any */ /** binds actions to dispatch */ export const useActions = ( actions: Actions ): BoundActionCreators => { const dispatch = useDispatch(); return useMemo( () => Object.entries(actions).reduce>((result, [key, fn]) => { (result as any)[key] = (...args: any[]) => dispatch(fn(...args)); return result; }, {} as any), [dispatch, ...Object.values(actions)] ) as any; }; export const useAction = (action: Action): BoundActionCreator => { const dispatch = useDispatch(); return useCallback(((...args: any[]) => dispatch(action(...args))) as any, [dispatch, action]); }; /* eslint-enable @typescript-eslint/no-explicit-any */