Skip to content

Commit ca13aeb

Browse files
committed
use-combine-reducers hook
1 parent 907b387 commit ca13aeb

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"react": "^16.8.6",
77
"react-dom": "^16.8.6",
88
"react-scripts": "3.0.1",
9+
"use-combined-reducers": "^1.0.3",
910
"uuid": "^3.3.2"
1011
},
1112
"scripts": {

src/App.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import React, {
66
} from 'react';
77
import uuid from 'uuid/v4';
88

9+
import useCombinedReducers from 'use-combined-reducers';
10+
911
const DispatchContext = createContext(null);
1012

1113
const initialTodos = [
@@ -68,24 +70,8 @@ const todoReducer = (state, action) => {
6870
}
6971
};
7072

71-
const useCombinedReducer = combinedReducers => {
72-
// Global State
73-
const state = Object.keys(combinedReducers).reduce(
74-
(acc, key) => ({ ...acc, [key]: combinedReducers[key][0] }),
75-
{}
76-
);
77-
78-
// Global Dispatch Function
79-
const dispatch = action =>
80-
Object.keys(combinedReducers)
81-
.map(key => combinedReducers[key][1])
82-
.forEach(fn => fn(action));
83-
84-
return [state, dispatch];
85-
};
86-
8773
const App = () => {
88-
const [state, dispatch] = useCombinedReducer({
74+
const [state, dispatch] = useCombinedReducers({
8975
filter: useReducer(filterReducer, 'ALL'),
9076
todos: useReducer(todoReducer, initialTodos),
9177
});

0 commit comments

Comments
 (0)