I am trying to update each element in an array using a for loop
When you click on the 'add all' button, it should start updating each element of the array
The update should happen one after the other, and should be visible in the UI
The array is stored in a useState hook
However, the array is not updating as expected
Here's the link to the CodeSandBox : https://codesandbox.io/s/heuristic-hooks-z3wq7?file=/src/App.js
export default function App() { const [users, setUsers] = useState(() => fakeUsers); function addHandler(id){ let arr = cloneDeep(users); let i = arr.findIndex((u) => u.id === id); arr[i].added = true; setUsers(arr); }; async function addAll() { for (let i = 0; i < users.length; i++) { let id = users[i].id; await delay(1000); addHandler(id); } } return ( <div className="App"> <button onClick={addAll}>add all</button> {users.map((e) => ( <div> {e.name}-{e.added ? "YES" : "NO"} </div> ))} </div> ); }