Usman Shahid
Usman Shahid

Reputation: 761

React useState is not immediately updating the state of an array (react-native)

I made this simple example to explain my problem:

Problem Link

In this example, message should be displayed when 5 is added but it is displaying in next event later to the event when 5 is added. I am new to React so i don't really understand hooks and states yet, i see some other examples similar questions but since i'm completely new to react, all that stuff is a kind of overwhelming for me, i just need the solution for this particular problem.

Upvotes: 1

Views: 57

Answers (1)

Egor Trussov
Egor Trussov

Reputation: 33

The problem is that useState and some other hooks are async to improve the perfomance. In order to fix the issue, you should use the useEffect hook whick will run as soon as a certain variable value changes

Upvotes: 2

Related Questions