Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- export class Clock extends React.Component {
- constructor(props) {
- super(props);
- this.state = { date: new Date() };
- }
- render() {
- return <div>{this.state.date.toLocaleTimeString()}</div>;
- }
- componentDidMount() {
- const oneSecond = 1000;
- this.intervalID = setInterval(() => { //Save the interval ID into a variable
- this.setState({ date: new Date() });
- }, oneSecond);
- }
- componentWillUnmount(){ //Add the new method that cleans up the interval
- clearInterval(this.intervalID);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement