Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import React from "react";
import logo from "./logo.png";
import "./App.css";
import Clock from "./Clock.js";

class App extends React.Component {
// constructor(props) {
// super(props)};
// // Initialise component state to contain "date" attribute with current date and time
// this.state = { date: new Date() };
// }
// componentDidMount() {
// // Set date value in state every second to current date and time
// // Save setInterval timer ID in class variable for teardown in another class method
// this.timerId = setInterval(() => {
// this.setState({
// date: new Date(),
// });
// }, 1000);
// }

// componentWillUnmount() {
// // Teardown setInterval timer with timer ID saved as class variable
// clearInterval(this.timerId);
// }
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<Clock timeZone="America/Los_Angeles" />
<Clock timeZone="Europe/London" />
<Clock timeZone="Asia/Singapore" />
</header>
</div>
);
}
}

export default App;
36 changes: 36 additions & 0 deletions src/Clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from "react";

class Clock extends React.Component {
constructor(props) {
super(props);
// Initialise component state to contain "date" attribute with current date and time
this.state = { date: new Date() };
}
componentDidMount() {
// Set date value in state every second to current date and time
// Save setInterval timer ID in class variable for teardown in another class method
this.timerId = setInterval(() => {
this.setState({
date: new Date(),
});
}, 1000);
}

componentWillUnmount() {
// Teardown setInterval timer with timer ID saved as class variable
clearInterval(this.timerId);
}

render() {
return (
<div>
<h1>
{`${this.props.timeZone}: ${this.state.date.toLocaleString("en-GB", {
timeZone: this.props.timeZone,
})}`}
</h1>
</div>
);
}
}
export default Clock;