Skip to content

Instantly share code, notes, and snippets.

@atomatt
Last active July 19, 2017 21:21
Show Gist options
  • Select an option

  • Save atomatt/dba318185686aeb2fe19c9e32c408511 to your computer and use it in GitHub Desktop.

Select an option

Save atomatt/dba318185686aeb2fe19c9e32c408511 to your computer and use it in GitHub Desktop.
Stupid Preact Example with no transpiling
<!DOCTYPE html>
<html>
<head>
<title>Preact Demo</title>
<style>
a {
text-decoration: underline;
cursor: pointer;
}
</style>
</head>
<body>
<script src="https://unpkg.com/preact"></script>
<script>
'use strict';
const { h, render, Component } = window.preact;
class Attack extends Component {
constructor() {
super();
this.state.weak = false;
}
toggleStrength() {
this.setState({weak: !this.state.weak});
}
render({name}, {weak}) {
let strength = h("a", {onclick: () => {this.toggleStrength();}},
weak ? "weak" : "normal");
return h("p", null, name, " (strength: ", strength, ")");
}
}
const Player = ({name, attacks}) => (
h("div", null,
h("h2", null, name),
attacks.map((a) => (h(Attack, a))),
)
)
const Main = () => {
const players = [
{
name: "Matt",
attacks: [
{name: "Flaming Arrow"},
],
},
{
name: "Simon",
attacks: [
{name: "Fluffy Cushion"},
],
},
];
return h("div", null, players.map((p) => { return h(Player, p); }));
}
render(h(Main), document.body);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment