Skip to content

Commit a0b8055

Browse files
committed
one state container
1 parent e6d475b commit a0b8055

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/App.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import uuid from 'uuid/v4';
88

99
const DispatchContext = createContext(null);
1010

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

7171
const App = () => {
7272
const [filter, dispatchFilter] = useReducer(filterReducer, 'ALL');
73-
const [todos, dispatchTodos] = useReducer(todoReducer, initalTodos);
73+
const [todos, dispatchTodos] = useReducer(
74+
todoReducer,
75+
initialTodos
76+
);
7477

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

79-
const filteredTodos = todos.filter(todo => {
80-
if (filter === 'ALL') {
82+
// Global State
83+
const state = {
84+
filter,
85+
todos,
86+
};
87+
88+
const filteredTodos = state.todos.filter(todo => {
89+
if (state.filter === 'ALL') {
8190
return true;
8291
}
8392

84-
if (filter === 'COMPLETE' && todo.complete) {
93+
if (state.filter === 'COMPLETE' && todo.complete) {
8594
return true;
8695
}
8796

88-
if (filter === 'INCOMPLETE' && !todo.complete) {
97+
if (state.filter === 'INCOMPLETE' && !todo.complete) {
8998
return true;
9099
}
91100

0 commit comments

Comments
 (0)