Skip to content

Instantly share code, notes, and snippets.

@dzek69
Created July 16, 2017 18:55
Show Gist options
  • Save dzek69/5c733cd20643947e87a333e437c362b3 to your computer and use it in GitHub Desktop.
Save dzek69/5c733cd20643947e87a333e437c362b3 to your computer and use it in GitHub Desktop.
Clock items
const React = require("react");
const { render } = require("react-dom");
const root = document.querySelector("#r");
// const log = (text) => {
// document.querySelector("#log").textContent += "\n" + text;
// }
// const heavyCalc = () => {
// const start = Date.now();
// while (Date.now() - start < 200) { }
// }
class Items extends React.Component {
render() {
const num = this.props.items ? this.props.items.length : 0;
return (
<div>
<h2>items</h2>
<p>got {num} items</p>
</div>
)
}
}
class Clock extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
unix: Date.now(),
}
this.refreshClock = this.refreshClock.bind(this);
}
componentDidMount() {
setTimeout(this.refreshClock, 1000)
}
refreshClock() {
this.setState({
unix: Date.now(),
})
setTimeout(this.refreshClock, 1000)
}
render() {
return (
<div style={{padding: "20px"}}>
<h1>unix timestamp is now: {this.state.unix}</h1>
<Items />
</div>
)
}
}
// render(<Clock items={[1, 2]} />, root);
render(<Clock />, root);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment