Created
April 23, 2021 03:16
-
-
Save benjaminoakes/3acdc70bca8c1b3b0881e2e581257b0f to your computer and use it in GitHub Desktop.
Preact and HTM example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { html, Component, render } from 'https://unpkg.com/htm/preact/standalone.module.js'; | |
class Clock extends Component { | |
constructor() { | |
super() | |
this.tick() | |
} | |
componentDidMount() { | |
this.timer = setInterval(() => this.tick(), 1000) | |
} | |
tick() { | |
this.setState({ time: new Date() }) | |
} | |
componentWillUnmount() { | |
clearInterval(this.timer) | |
} | |
render() { | |
let iso8601 = this.state.time.toISOString() | |
return html`<span>${iso8601}</span>` | |
} | |
} | |
render(html`<${Clock} />`, document.body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment