Skip to content
Advertisement

react native useState is not incrementing counter

When counter is >= data Array length setLoading should trigger. But counter is not being incrementing until I refresh app by pressing CTRL+S then counter increment itself by 1 every time app refreshes.

JavaScript

Advertisement

Answer

Updating state is an asynchronous operation, so getting loopCount in for loop will get the initial loopCount value instead of updated value. If you don’t need UI update for updating loopCount, you can use useRef instead of useState:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement