title: Why previous state in SetSate hook is turning out to be undefined
body: <p>Code Snippet:</p>
  <pre><code>const [users,setUsers]=useState([]);
  
  useEffect(()=&gt;{
    for(let x in array){
      var ref = firebase.database().ref(`/users/${array[x]}`);
        ref.on('value', snapshot =&gt; {
          let data = snapshot.val();
           
           setUsers((prev)=&gt;[...prev,data])
    }
  },[])
  </code></pre>
  <p><code>array</code> contains list of user ids.
  Data contains the right value returned by snapshot.val().</p>
  <p>If I do <code>console.log(users)</code> the 1st line inside useEffect, it is an empty array which is the expected value.</p>
  <p>I don't understand why <code>prev</code> is  turning out to be undefined in <code>setUsers</code></p>