Skip to content

Commit 9580679

Browse files
committed
dispatch with context for todo
1 parent d024a24 commit 9580679

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/App.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import React, { useState, useReducer } from 'react';
1+
import React, {
2+
useState,
3+
useReducer,
4+
useContext,
5+
createContext,
6+
} from 'react';
27
import uuid from 'uuid/v4';
38

9+
const TodoContext = createContext(null);
10+
411
const initalTodos = [
512
{
613
id: uuid(),
@@ -82,11 +89,11 @@ const App = () => {
8289
});
8390

8491
return (
85-
<div>
92+
<TodoContext.Provider value={dispatchTodos}>
8693
<Filter dispatch={dispatchFilter} />
87-
<TodoList dispatch={dispatchTodos} todos={filteredTodos} />
88-
<AddTodo dispatch={dispatchTodos} />
89-
</div>
94+
<TodoList todos={filteredTodos} />
95+
<AddTodo />
96+
</TodoContext.Provider>
9097
);
9198
};
9299

@@ -118,15 +125,17 @@ const Filter = ({ dispatch }) => {
118125
);
119126
};
120127

121-
const TodoList = ({ dispatch, todos }) => (
128+
const TodoList = ({ todos }) => (
122129
<ul>
123130
{todos.map(todo => (
124-
<TodoItem key={todo.id} dispatch={dispatch} todo={todo} />
131+
<TodoItem key={todo.id} todo={todo} />
125132
))}
126133
</ul>
127134
);
128135

129-
const TodoItem = ({ dispatch, todo }) => {
136+
const TodoItem = ({ todo }) => {
137+
const dispatch = useContext(TodoContext);
138+
130139
const handleChange = () =>
131140
dispatch({
132141
type: todo.complete ? 'UNDO_TODO' : 'DO_TODO',
@@ -147,7 +156,9 @@ const TodoItem = ({ dispatch, todo }) => {
147156
);
148157
};
149158

150-
const AddTodo = ({ dispatch }) => {
159+
const AddTodo = () => {
160+
const dispatch = useContext(TodoContext);
161+
151162
const [task, setTask] = useState('');
152163

153164
const handleSubmit = event => {

0 commit comments

Comments
 (0)