core-native
Version:
A lightweight framework based on React Native + Redux + Redux Saga, in strict TypeScript.
47 lines • 1.69 kB
JavaScript
import React from "react";
import { useDispatch, useSelector } from "react-redux";
export function useLoadingStatus(identifier = "global") {
return useSelector((state) => state.loading[identifier] > 0);
}
/**
* Action parameters must be of primitive types, so that the dependency check can work well.
* No need add dispatch to dep list, because it is always fixed.
*/
export function useAction(actionCreator, ...deps) {
const dispatch = useDispatch();
return React.useCallback(() => dispatch(actionCreator(...deps)), deps);
}
/**
* For actions like:
* *foo(a: number, b: string, c: boolean): SagaGenerator {..}
*
* useUnaryAction(foo, 100, "") will return:
* (c: boolean) => void;
*/
export function useUnaryAction(actionCreator, ...deps) {
const dispatch = useDispatch();
return React.useCallback((arg) => dispatch(actionCreator(...deps, arg)), deps);
}
/**
* For actions like:
* *foo(a: number, b: string, c: boolean): SagaGenerator {..}
*
* useBinaryAction(foo, 100) will return:
* (b: string, c: boolean) => void;
*/
export function useBinaryAction(actionCreator, ...deps) {
const dispatch = useDispatch();
return React.useCallback((arg1, arg2) => dispatch(actionCreator(...deps, arg1, arg2)), deps);
}
/**
* For actions like:
* *foo(data: {key: number}): SagaGenerator {..}
*
* useModuleObjectAction(foo, "key") will return:
* (objectValue: number) => void;
*/
export function useObjectKeyAction(actionCreator, objectKey) {
const dispatch = useDispatch();
return React.useCallback((objectValue) => dispatch(actionCreator({ [objectKey]: objectValue })), [dispatch, actionCreator, objectKey]);
}
//# sourceMappingURL=hooks.js.map