0

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)
    })
  }
HannahO
  • 3
  • 2
  • 1
    Does this answer your question? [The useState set method is not reflecting a change immediately](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) – Brian Thompson May 31 '22 at 14:40
  • @BrianThompson, No unfortunately I've tried the solutions in this answer and they haven't worked – HannahO May 31 '22 at 16:41

0 Answers0