Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 779 Bytes

reactjs.md

File metadata and controls

40 lines (33 loc) · 779 Bytes
  1. Write a program to display current date which updates every second using react
class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
        <h1>{this.state.date.toLocaleTimeString()}.</h1>
    );
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);

Details about how this code works can be found on the react site