UNPKG

usemakedispatchable

Version:

a react hook that takes a set of redux reducers and returns the same input reducers but wrapped with the redux disptch function ready to call

24 lines (23 loc) 937 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_redux_1 = require("react-redux"); const checktypes_1 = require("@samislam/checktypes"); function useMakeDispatchable(reducers) { const dispatch = (0, react_redux_1.useDispatch)(); switch (true) { case (0, checktypes_1.isArray)(reducers): { const DispatchableList = reducers.map((reducer) => (...arg) => dispatch(reducer(...arg))); return DispatchableList; } case (0, checktypes_1.isObject)(reducers): { const DispatchableList = {}; for (const [key, value] of Object.entries(reducers)) { DispatchableList[key] = (...arg) => dispatch(value(...arg)); } return DispatchableList; } default: // isAsycOrSyncFunc return (...arg) => dispatch(reducers(...arg)); } } exports.default = useMakeDispatchable;