I'm having trouble with the code given under. I want to add a new task object to my array, so firstly i try to find the max id in the tasks array and i give the maxId+1 to my new object as id. Then i try to append my new Task object at the end, but data is not updated after i call setTasks.
Any helps would be appreciated
const[tasks,setTasks] = useState([
{
id:1,
text:"Task1",
day:"21.06.2021",
reminder : true
},
{
id:2,
text:"Task2",
day:"21.06.2021",
reminder : false
},
{
id:3,
text:"Task3",
day:"22.06.2021",
reminder : true
}
])
const addTask = (task) => {
var id = 0
for (const t of tasks){
if (t.id > id){
id = t.id+1
}
}
console.log("Before",tasks)
const newTask = {id, ...task}
setTasks([...tasks, newTask])
console.log("After",tasks)
}