简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM