source: imaps-frontend/node_modules/react-use/lib/useMethods.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.1 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var react_1 = require("react");
4var useMethods = function (createMethods, initialState) {
5 var reducer = react_1.useMemo(function () { return function (reducerState, action) {
6 var _a;
7 return (_a = createMethods(reducerState))[action.type].apply(_a, action.payload);
8 }; }, [createMethods]);
9 var _a = react_1.useReducer(reducer, initialState), state = _a[0], dispatch = _a[1];
10 var wrappedMethods = react_1.useMemo(function () {
11 var actionTypes = Object.keys(createMethods(initialState));
12 return actionTypes.reduce(function (acc, type) {
13 acc[type] = function () {
14 var payload = [];
15 for (var _i = 0; _i < arguments.length; _i++) {
16 payload[_i] = arguments[_i];
17 }
18 return dispatch({ type: type, payload: payload });
19 };
20 return acc;
21 }, {});
22 }, [createMethods, initialState]);
23 return [state, wrappedMethods];
24};
25exports.default = useMethods;
Note: See TracBrowser for help on using the repository browser.