@@ -6,7 +6,7 @@ import React, {
66} from 'react' ;
77import uuid from 'uuid/v4' ;
88
9- const TodoContext = createContext ( null ) ;
9+ const DispatchContext = createContext ( null ) ;
1010
1111const initalTodos = [
1212 {
@@ -35,7 +35,7 @@ const filterReducer = (state, action) => {
3535 case 'SHOW_INCOMPLETE' :
3636 return 'INCOMPLETE' ;
3737 default :
38- throw new Error ( ) ;
38+ return state ;
3939 }
4040} ;
4141
@@ -64,14 +64,18 @@ const todoReducer = (state, action) => {
6464 complete : false ,
6565 } ) ;
6666 default :
67- throw new Error ( ) ;
67+ return state ;
6868 }
6969} ;
7070
7171const App = ( ) => {
7272 const [ filter , dispatchFilter ] = useReducer ( filterReducer , 'ALL' ) ;
7373 const [ todos , dispatchTodos ] = useReducer ( todoReducer , initalTodos ) ;
7474
75+ // Global Dispatch Function
76+ const dispatch = action =>
77+ [ dispatchTodos , dispatchFilter ] . forEach ( fn => fn ( action ) ) ;
78+
7579 const filteredTodos = todos . filter ( todo => {
7680 if ( filter === 'ALL' ) {
7781 return true ;
@@ -89,15 +93,17 @@ const App = () => {
8993 } ) ;
9094
9195 return (
92- < TodoContext . Provider value = { dispatchTodos } >
93- < Filter dispatch = { dispatchFilter } />
96+ < DispatchContext . Provider value = { dispatch } >
97+ < Filter />
9498 < TodoList todos = { filteredTodos } />
9599 < AddTodo />
96- </ TodoContext . Provider >
100+ </ DispatchContext . Provider >
97101 ) ;
98102} ;
99103
100- const Filter = ( { dispatch } ) => {
104+ const Filter = ( ) => {
105+ const dispatch = useContext ( DispatchContext ) ;
106+
101107 const handleShowAll = ( ) => {
102108 dispatch ( { type : 'SHOW_ALL' } ) ;
103109 } ;
@@ -134,7 +140,7 @@ const TodoList = ({ todos }) => (
134140) ;
135141
136142const TodoItem = ( { todo } ) => {
137- const dispatch = useContext ( TodoContext ) ;
143+ const dispatch = useContext ( DispatchContext ) ;
138144
139145 const handleChange = ( ) =>
140146 dispatch ( {
@@ -157,7 +163,7 @@ const TodoItem = ({ todo }) => {
157163} ;
158164
159165const AddTodo = ( ) => {
160- const dispatch = useContext ( TodoContext ) ;
166+ const dispatch = useContext ( DispatchContext ) ;
161167
162168 const [ task , setTask ] = useState ( '' ) ;
163169
0 commit comments