Answer the question
In order to leave comments, you need to log in
Editing not working, how to solve?
I want to add an editing function to the todo app, but when I change a note, undefined comes up.
Here is the action creator:
export const changeTodo = (id, text) => {
debugger
return {
type: CHANGE_TODO,
id,
text
}
}
case CHANGE_TODO:
debugger
return {
...state,
todos: state.todos.map(todo => todo.id === action.id
? { ...todo, id: todo.id, text: action.text, completed: todo.completed }
: todo
)
}
const TodoList = ({removeTodo, toggleTodo, changeTodo, todos}) => {
return (
<StyledTodoList>
<Ul>
{
todos.map(todo =>
<Todo
key={todo.id}
toggleHandler={() => toggleTodo(todo.id)}
removeHandler={() => removeTodo(todo.id)}
сhangeHandler={() => changeTodo(todo.id)}
// для для изменения заметки нужно добавит еще один параметр, который будет новым значением
{...todo}
/>
)
}
</Ul>
<AddButton>
<NavLink
to='addTodo'
>+</NavLink>
</AddButton>
</StyledTodoList>
)
}
const Todo = props => {
const { id, text, toggleHandler, removeHandler, сhangeHandler} = props
const [isEditingStarted, setEditValue] = useState(false)
const [inputValue, setInputValue] = useState('')
const submitHandler = event => {
event.preventDefault()
сhangeHandler(id, inputValue)
setInputValue('')
}
return (
<StyledTodo {...props}>
<StyledCheckBox
type='checkbox'
onClick={toggleHandler}
/>
{isEditingStarted ?
<form onSubmit={submitHandler}>
<Input
defaultValue={text}
onChange={e => setInputValue(e.target.value)}
onKeyDown = {e => e.keyCode === 27 ? setEditValue(false) : ''}
/>
</form> :
text
}
<div>
<Span
onClick={() => setEditValue(true)}
>
<img
src={changeIcon}
alt='Ред'
/>
</Span>
<Span
onClick={removeHandler}
>
<img
src={closeIcon}
alt='Закрыть'
/>
</Span>
</div>
</StyledTodo>
)
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question