You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/frontend/src/@redux/hooks/base.ts

37 lines
1.0 KiB

import { useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { createCallbackAction } from "../actions/factory";
import { ActionCallback, AsyncActionDispatcher } from "../types";
// function use
export function useReduxStore<T extends (store: ReduxStore) => any>(
selector: T
) {
return useSelector<ReduxStore, ReturnType<T>>(selector);
}
export function useReduxAction<T extends (...args: any[]) => void>(action: T) {
const dispatch = useDispatch();
return useCallback((...args: Parameters<T>) => dispatch(action(...args)), [
action,
dispatch,
]);
}
export function useReduxActionWith<
T extends (...args: any[]) => AsyncActionDispatcher<any>
>(action: T, success: ActionCallback) {
const dispatch = useDispatch();
return useCallback(
(...args: Parameters<T>) => {
const callbackAction = createCallbackAction(
() => [action(...args)],
success
);
dispatch(callbackAction());
},
[dispatch, action, success]
);
}