react native show current time and update the seconds in real-time

前端 未结 6 1835
说谎
说谎 2021-02-05 05:53

I want to show the current time(MM/DD/YY hh:mm:ss) in react native app like a clock, and get update every seconds, I tried using new Date() and set it in state, but the time don

6条回答
  •  -上瘾入骨i
    2021-02-05 06:33

    I got the answer. The code below also works.

    componentWillMount(){
        setInterval(function(){
            this.setState({
                curTime: new Date().toLocaleString()
            })
        }.bind(this), 1000);
    }
    

提交回复
热议问题