1- import React from 'react' ;
1+ import React , { useContext , memo } from 'react' ;
22import useToggle from './hooks/useToggle' ;
3+ import { DispatchContext } from './contexts/todos.context' ;
34import EditTodo from './EditTodo' ;
45import ListItem from '@material-ui/core/ListItem' ;
56import ListItemText from '@material-ui/core/ListItemText' ;
@@ -8,18 +9,20 @@ import IconButton from '@material-ui/core/IconButton';
89import DeleteIcon from '@material-ui/icons/Delete' ;
910import EditIcon from '@material-ui/icons/Edit' ;
1011import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction' ;
11- function TodoItem ( { id, task, completed, deleteTodo , toggleComplete , editTodo } ) {
12+ function TodoItem ( { id, task, completed} ) {
1213 const [ isEditing , toggle ] = useToggle ( false ) ;
14+ const dispatch = useContext ( DispatchContext ) ;
15+ console . log ( "TODO Rerendering" , id ) ;
1316 return (
1417 < ListItem style = { { height : "64px" } } >
15- { isEditing ? < EditTodo editTodo = { editTodo } id = { id } task = { task } toggle = { toggle } /> : (
18+ { isEditing ? < EditTodo id = { id } task = { task } toggle = { toggle } /> : (
1619 < >
17- < Checkbox tabIndex = { - 1 } checked = { completed } onClick = { ( ) => toggleComplete ( id ) } />
20+ < Checkbox tabIndex = { - 1 } checked = { completed } onClick = { ( ) => dispatch ( { type : "TOGGLE" , id : id } ) } />
1821 < ListItemText style = { { textDecoration : completed ? 'line-through' : 'none' } } >
1922 { task }
2023 </ ListItemText >
2124 < ListItemSecondaryAction >
22- < IconButton aria-label = 'Delete' onClick = { ( ) => deleteTodo ( id ) } >
25+ < IconButton aria-label = 'Delete' onClick = { ( ) => dispatch ( { type : "DELETE" , id : id } ) } >
2326 < DeleteIcon />
2427 </ IconButton >
2528 < IconButton aria-label = 'Edit' onClick = { toggle } >
@@ -33,4 +36,4 @@ function TodoItem({id, task, completed, deleteTodo, toggleComplete, editTodo}){
3336 )
3437}
3538
36- export default TodoItem ;
39+ export default memo ( TodoItem ) ;
0 commit comments