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

How to handle setInverval and clearInterval on react native

$
0
0

I want to change the value of state [time] every second when a button is pressed. I have the code below, but unfortunately the code doesn't work.Do you have any suggestion on how can I handle this issue ?

Many thanks !

import React,{useState} from 'react';import {View,Text,StyleSheet,TouchableOpacity} from 'react-native';import { AntDesign } from '@expo/vector-icons';const TimeButton = () =>{  const [time,setTime] = useState(10)  let intervalId:any = null;  return(<View style={styles.container}><TouchableOpacity><AntDesign name="caretleft" size={24} color="black" /></TouchableOpacity><Text>{time}</Text><TouchableOpacity onPressIn={() => {        intervalId = setInterval(setTime(time+1), 1000)        }} onPressOut={() => clearInterval(intervalId)}><AntDesign name="caretright" size={59} color="black" /></TouchableOpacity></View>  )}const styles = StyleSheet.create({  container:{    flex:1,    flexDirection:'row',    alignItems:'center',    justifyContent:"center"  }})export default TimeButton;

Viewing all articles
Browse latest Browse all 6214

Trending Articles



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