Simple counter in React Native

zzbbenben 注册会员
2023-01-25 12:51

The issue is with setA(a++). The state should never be set in such a way that it can cause a re-render causing it to set state again (which is happening above). Simply put, setA increments the value of a, which causes setA to run again which again increments the value of a, and so on and so forth.

So, if you just want the state to be set only once, you can place it in a useEffect

useEffect(() => setA(a++), []);

But most of the time, you'd need to use buttons or stuff like that to update the state.

About the Author

Question Info

Publish Time
2023-01-25 12:51
Update Time
2023-01-25 12:51