Reputation: 33
var timer = 0;
const [run, setRun] = useState(false);
useEffect(() => {
if (run) {
Toast.show("Timmer is running"+timer, Toast.LONG);
var interval = setInterval(() => { timer++; }, 1000);
} else {
Toast.show("Timmer is stopped"+timer, Toast.LONG);
clearInterval(interval);
}
}, [run]);
const runTimer = () => {
setRun(!run);
}
//JSX
<Text onPress={runTimer}
style={{fontSize: 18, color: 'black', zIndex: 11, fontFamily: 'Roboto-Bold',}}>
{timer}
</Text>
Here , while i click on text , i need the timer to start and pause when i click back , but its not updating timer at all.
Can someone help me out, Thank you.
Upvotes: 0
Views: 124
Reputation: 81
timer is unable to receive the current timer value, i think you can declare timer
as a state variable and then setInterval(() => { setTimer(prev=>prev+1 }, 1000);
Upvotes: 2