I want to loop through an array, and call a function for each item, which does setState. I tried the following code:
const approveOrder = uniqueId => {
if (approvedList.indexOf(uniqueId) < 0) {
setApprovedList(approvedList.concat(uniqueId));
}
};
const approveAllOrders = data => {
data.forEach(dataItem => {
approveOrder(dataItem.unique_id);
});
};
But after the above function approveAllOrders, my list approvedList 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?