Skip to content
Permalink
Browse files

one state container

  • Loading branch information...
rwieruch committed May 13, 2019
1 parent e6d475b commit a0b8055d6e04f1496d8bdc185cb27ccd6ad3bb8a
Showing with 15 additions and 6 deletions.
  1. +15 −6 src/App.js
@@ -8,7 +8,7 @@ import uuid from 'uuid/v4';

const DispatchContext = createContext(null);

const initalTodos = [
const initialTodos = [
{
id: uuid(),
task: 'Learn React',
@@ -70,22 +70,31 @@ const todoReducer = (state, action) => {

const App = () => {
const [filter, dispatchFilter] = useReducer(filterReducer, 'ALL');
const [todos, dispatchTodos] = useReducer(todoReducer, initalTodos);
const [todos, dispatchTodos] = useReducer(
todoReducer,
initialTodos
);

// Global Dispatch Function
const dispatch = action =>
[dispatchTodos, dispatchFilter].forEach(fn => fn(action));

const filteredTodos = todos.filter(todo => {
if (filter === 'ALL') {
// Global State
const state = {
filter,
todos,
};

const filteredTodos = state.todos.filter(todo => {
if (state.filter === 'ALL') {
return true;
}

if (filter === 'COMPLETE' && todo.complete) {
if (state.filter === 'COMPLETE' && todo.complete) {
return true;
}

if (filter === 'INCOMPLETE' && !todo.complete) {
if (state.filter === 'INCOMPLETE' && !todo.complete) {
return true;
}

0 comments on commit a0b8055

Please sign in to comment.
You can’t perform that action at this time.