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

前端 未结 6 1836
说谎
说谎 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:36

    Full Code here:

    import React, { Component } from 'react';
    import { Text, View } from 'react-native';
    
    
    export default class KenTest extends Component {
    
      componentDidMount(){ 
    
        setInterval(() => (
    
          this.setState(
            { curTime : new Date().toLocaleString()}
    
          )
        ), 1000);
      }
    
    
    state = {curTime:new Date().toLocaleString()};
    
    
      render() {
        return (
          
      {'\n'}{'\n'}{'\n'}The time is: {this.state.curTime}
    
          
        );
      }
    }
    

提交回复
热议问题