Preact Clock
by Jason Miller
HTML
<script src="https://npmcdn.com/preact@latest/dist/preact.js"></script>
Babel + JSX
/** @jsx h */
const { h, render, Component } = preact; // normally this would be an import statement.
class Clock extends Component {
constructor() {
super();
// set initial time:
this.state.time = Date.now();
}
componentDidMount() {
// update time every second
this.timer = setInterval(() => {
this.setState({ time: Date.now() });
}, 1000);
}
componentWillUnmount() {
// stop when not renderable
clearInterval(this.timer);
}
render(props, state) {
let time = new Date(state.time).toLocaleTimeString();
return <span>{ time }</span>;
}
}
// render an instance of Clock into <body>:
render(<Clock />, document.body);