@anhnh27/reduxtoolkit-action-dispatcher
Version:
Dispatch redux actions without useDispatch or mapDispatchToProps
29 lines (28 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const actionDispatcherMiddleware_1 = require("./actionDispatcherMiddleware");
const utils_1 = require("./utils");
const createDispatcher = (slice) => {
const dispatcher = {};
const actions = slice.actions;
for (const action in actions) {
if (Object.prototype.hasOwnProperty.call(actions, action)) {
Object.defineProperty(dispatcher, action, {
value: wrapDispatch(`${slice.name}/${action}`, actions[action]),
});
}
}
return dispatcher;
};
const wrapDispatch = (type, creator) => {
const dispatcher = (...args) => {
const originalAction = creator.apply(null, args);
let action = (0, utils_1.injectResultToAction)(originalAction);
actionDispatcherMiddleware_1.store.dispatch(action);
return action;
};
dispatcher.toString = () => type;
dispatcher.type = type;
return dispatcher;
};
exports.default = createDispatcher;