Skip to content

Commit

Permalink
use-combine-reducers hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed May 13, 2019
1 parent 907b387 commit ca13aeb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"use-combined-reducers": "^1.0.3",
"uuid": "^3.3.2"
},
"scripts": {
Expand Down
20 changes: 3 additions & 17 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import React, {
} from 'react';
import uuid from 'uuid/v4';

import useCombinedReducers from 'use-combined-reducers';

const DispatchContext = createContext(null);

const initialTodos = [
Expand Down Expand Up @@ -68,24 +70,8 @@ const todoReducer = (state, action) => {
}
};

const useCombinedReducer = combinedReducers => {
// Global State
const state = Object.keys(combinedReducers).reduce(
(acc, key) => ({ ...acc, [key]: combinedReducers[key][0] }),
{}
);

// Global Dispatch Function
const dispatch = action =>
Object.keys(combinedReducers)
.map(key => combinedReducers[key][1])
.forEach(fn => fn(action));

return [state, dispatch];
};

const App = () => {
const [state, dispatch] = useCombinedReducer({
const [state, dispatch] = useCombinedReducers({
filter: useReducer(filterReducer, 'ALL'),
todos: useReducer(todoReducer, initialTodos),
});
Expand Down

0 comments on commit ca13aeb

Please sign in to comment.