diff --git a/src/App.js b/src/App.js index 4a6f800f..c096cda1 100644 --- a/src/App.js +++ b/src/App.js @@ -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 (
logo -

- Edit src/App.js and save to reload. -

+ + +
); } } - export default App; diff --git a/src/Clock.js b/src/Clock.js new file mode 100644 index 00000000..7eb462c6 --- /dev/null +++ b/src/Clock.js @@ -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 ( +
+

+ {`${this.props.timeZone}: ${this.state.date.toLocaleString("en-GB", { + timeZone: this.props.timeZone, + })}`} +

+
+ ); + } +} +export default Clock;