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

前端 未结 6 1837
说谎
说谎 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条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 06:29

    Just move setInterval into componentDidMount function.

    Like this :

      componentDidMount() {
        setInterval(() => {
          this.setState({
            curTime : new Date().toLocaleString()
          })
        }, 1000)
      }
    

    This will change state and update every 1s.

提交回复
热议问题