1

on this code setInterval() does not work and just gives the time once! Also i'm learning and i just want to use component not function component.

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class Tick extends Component {
     constructor(){
          super();
          this.state ={result: ''}
     }
     timer= () => {
         this.setState({result: <h2>It is {new Date().toLocaleTimeString()}.</h2>})  
     }
     inter = () => {
          setInterval(this.timer(), 1000);
     }
     render() {
          return (
          <div>
              <h1>Hello, world!</h1>
              {this.state.result}
              <button onClick={this.inter} >start</button>
          </div>
          );
     }    
}

ReactDOM.render(<Tick />, document.getElementById('root'));
P...
  • 11
  • 5

0 Answers0