28
loading...
This website collects cookies to deliver better user experience
const updateTodo = ({taskId, todoId, value}) => {
setProject({
tasks: {
...state.tasks,
[taskId]: {
...state.tasks[taskId],
todos: {
...state.tasks[taskId].todos,
[todoId]: {
value: value
}
}
}
}
})
}
project.tasks[taskId].todos[todoId].value = true
Immutability makes complex features much easier to implement. Avoiding direct data mutation lets us keep previous versions of the state history intact, and reuse them later.
Detecting changes in mutable objects is difficult because they are modified directly. This detection requires the mutable object to be compared to previous copies of itself and the entire object tree to be traversed.
The main benefit of immutability is that it helps you build pure components in React. Immutable data can easily determine if changes have been made, which helps to determine when a component requires re-rendering.
const updateTodo = ({taskId, todoId, value}) => {
setState(produce(baseState, draftState => {
draftState.tasks[taskId].todos[todoId].value = value
return draftState
}))
}