I am trying to change the state of an array upon a click event. I have read up on immutability so have created a copy of the array in the method and mutated the copy rather than directly mutating the original. However the state still doesn’t update on first click. Is there something I’m doing wrong?
const [details, setDetails] = useState([]);
const getMovieDetails = () =>{
fetch("url")
.then((res)=> {
return res.json();
})
.then((data)=>{
const newDetails =[...details];
newDetails.push(data)
setDetails(newDetails)
console.log(details)
})
}