setInterval in a React app

前端 未结 7 524
天涯浪人
天涯浪人 2020-11-30 20:46

I\'m still fairly new at React, but I\'ve been grinding along slowly and I\'ve encountered something I\'m stuck on.

I am trying to build a \"timer\" component in Re

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 21:20

    Updating state every second in the react class. Note the my index.js passes a function that return current time.

    import React from "react";
    
    class App extends React.Component {
      constructor(props){
        super(props)
    
        this.state = {
          time: this.props.time,
    
        }        
      }
      updateMe() {
        setInterval(()=>{this.setState({time:this.state.time})},1000)        
      }
      render(){
      return (
        

    {this.state.time()}

    ); } } export default App;

提交回复
热议问题