Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

setInterval refreshes the app instead of updating the state

$
0
0

I'm coding a countdown with Expo. I'm using functional components, so my state is handled via React's useState hook.

let [state, setState] = useState({
    secondsLeft: 25,
    started: false,
});

If I press a Button it does fire this function:

let onPressHandler = (): void => {
    if(!state.started) {
        setState({...state, started: true});
        setInterval(()=> {
            setState({...state, secondsLeft: state.secondsLeft - 1});
            console.log(state.secondsLeft);
        }, 1000);
    }
}

Problem is that each 1000 ms Expo refreshes the app instead of updating the state.

enter image description here

Can you help me, please?


Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>