DEV Community

Discussion on: React cant reach current state

justincy profile image

You can get the latest value by passing a function to setUsers and it will be passed the current value as a parameter. See

mertyjsx profile image
mertyjsx Author

can you look at this,
what is the reason of this

  1. row console.log(users) getting empty array
justincy profile image
Justin • Edited on

Because of a stale closure, following the rules of Javascript closures and scope.

Here's what you want:

  const delet = (id) => {
    setUsers((currentUsers) => {
      return currentUsers.filter((i) => !== id);
Enter fullscreen mode Exit fullscreen mode