Skip to content

Instantly share code, notes, and snippets.

@terrancebryant
Created December 24, 2017 06:55
Show Gist options
  • Save terrancebryant/873dfdc0d9db1d020022dfbf062e2b59 to your computer and use it in GitHub Desktop.
Save terrancebryant/873dfdc0d9db1d020022dfbf062e2b59 to your computer and use it in GitHub Desktop.
JS Bin React Clock // source https://jsbin.com/revuqag
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="React Clock">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="root"></div>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<script id="jsbin-javascript">
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 (
React.createElement("div", null,
React.createElement("h1", null, "Hello, world!"),
React.createElement("h2", null, "It is ", this.state.date.toLocaleTimeString(), ".")
)
);
}
}
function App() {
return (
React.createElement("div", null,
React.createElement(Clock, null)
)
)
}
ReactDOM.render(
React.createElement(App, null),
document.getElementById('root')
);
</script>
<script id="jsbin-source-javascript" type="text/javascript">
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 (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
function App() {
return (
<div>
<Clock />
</div>
)
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
</script></body>
</html>
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 (
React.createElement("div", null,
React.createElement("h1", null, "Hello, world!"),
React.createElement("h2", null, "It is ", this.state.date.toLocaleTimeString(), ".")
)
);
}
}
function App() {
return (
React.createElement("div", null,
React.createElement(Clock, null)
)
)
}
ReactDOM.render(
React.createElement(App, null),
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment