I have a state which consists of array of Exam objects one of parameters of it (tasks) is array of objects too. I need to change parameter (status) of one item of parameter array (tasks) in one object (exam) in immutable way. Would appreciate you help.
const changeTaskStatus = useCallback((id) =>{
setExams(prev => prev.map((exam) =>{
if (exam.tasks.find(task => task.id === id)) {
const examCopy = {...exam};
examCopy.tasks.map(task => {
if (task.id === id) {
const taskCopy = {...task};
task.status = true;
return taskCopy
} else {
return task
}
})
return examCopy;
} else {
return exam
}
}))
}, [setExams])