title: How to to setState in a loop?
body: <p>I want to loop through an array, and call a function for each item, which does setState. I tried the following code:</p>
  <pre><code>const approveOrder = uniqueId =&gt; {
      if (approvedList.indexOf(uniqueId) &lt; 0) {
          setApprovedList(approvedList.concat(uniqueId));
      }
  };
  
  const approveAllOrders = data =&gt; {
      data.forEach(dataItem =&gt; {
          approveOrder(dataItem.unique_id);
      });
  };
  </code></pre>
  <p>But after the above function <code>approveAllOrders</code>, my list <code>approvedList</code> only contains one item, which is the last item of my data, which I passed to the function. I know that setState is not instant, that's why it is happening. So, what can I do to make my code work?</p>