why usestate prints in a different order

Viewed 29

why doesnt this go sequentially like the first run 1a>1b>2a>2b

also why does prevstate changes in 2b even though new render hasnt happened? I'm new to react and having very hard time understanding all these

 const [vald, setVald] = useState("d");

  useEffect(()=>{
    console.log("1a",vald)
    setVald(prevstate=>(
      console.log("1b",prevstate),
      "d1"
      ))
  },[vald])
  useEffect(()=>{
    console.log("2a",vald)
    setVald(prevstate=>(
      console.log("2b",prevstate),
      "d2"
      ))
  },[vald])
  console.log("render")

the output is

render
1a d 
1b d 
2a d 
2b d1
render
1a d2 
2a d2 
1b d2 
2b d1
render
0 Answers
Related