main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1001 bytes
|
Line | |
---|
1 | import { useMemo, useReducer } from 'react';
|
---|
2 | var useMethods = function (createMethods, initialState) {
|
---|
3 | var reducer = useMemo(function () { return function (reducerState, action) {
|
---|
4 | var _a;
|
---|
5 | return (_a = createMethods(reducerState))[action.type].apply(_a, action.payload);
|
---|
6 | }; }, [createMethods]);
|
---|
7 | var _a = useReducer(reducer, initialState), state = _a[0], dispatch = _a[1];
|
---|
8 | var wrappedMethods = useMemo(function () {
|
---|
9 | var actionTypes = Object.keys(createMethods(initialState));
|
---|
10 | return actionTypes.reduce(function (acc, type) {
|
---|
11 | acc[type] = function () {
|
---|
12 | var payload = [];
|
---|
13 | for (var _i = 0; _i < arguments.length; _i++) {
|
---|
14 | payload[_i] = arguments[_i];
|
---|
15 | }
|
---|
16 | return dispatch({ type: type, payload: payload });
|
---|
17 | };
|
---|
18 | return acc;
|
---|
19 | }, {});
|
---|
20 | }, [createMethods, initialState]);
|
---|
21 | return [state, wrappedMethods];
|
---|
22 | };
|
---|
23 | export default useMethods;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.