Answer the question
In order to leave comments, you need to log in
React.StrictMode breaking app?
The usual todo application . There is a function that, when clicked, puts a label on an completed
element from the list
const toggleHandler = (id) => {
setTodos(prevState => {
return prevState.map(todo => { // проблема тут
if (todo.id === id) todo.completed = !todo.completed
return todo
})
})
}
setTodos
it is called twice, putting down first completed: true
and then completed: false
and it looks like nothing is happening. Answer the question
In order to leave comments, you need to log in
Inside map do this
if (todo.id === id) todo.completed = !todo.completed
return todo
Strict mode can't automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following methods:
...
setState updater functions (the first argument)
...
if (todo.id === id) todo.completed = !todo.completed
it's a side effect. if (todo.id === id) return { ...todo, completed: !todo.completed }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question