score:1

Accepted answer

you are correct. setstate is async, and you are trying to fetch weather without waiting for that setstate to finish.


  <button onclick={() => {
cityprop("london")
 setworldcity("london") //<--- will resolve later, useeffect with catch the result.
}>

you can also do this:

  <button onclick={() => {
const newcity = "london" //<--- newcity is now the current city. use this inside of this function as worldcity will be stale until this function finishes.
cityprop(newcity)
 setworldcity(newcity) 
}>


Related Query

More Query from same tag