class Counter {
getInitialState () {
return {
count: 0
}
}
increment () {
return {
count: this.state.count + 1
}
}
decrement () {
return {
count: this.state.count - 1
}
}
render () {
return [
this.state.count,
h('button', {onClick: this.increment}, '+'),
h('button', {onClick: this.decrement}, '-')
]
}
}
dio.render(Counter);
And here is the same in Hyperapp: app({
state: 0,
actions: {
add: state => state + 1,
sub: state => state - 1
},
view: (state, actions) =>
<div>
<button
<h1>{state}</h1>
<button
</div>
}) app({
state: {
count: 0
},
view: (state, actions) =>
h("main", {}, [
h("h1", {}, [state.count]),
h("button", { onclick: actions.down }, "-"),
h("button", { onclick: actions.up }, "+"),
]),
actions: {
down: state => ({ count: state.count - 1 }),
up: state => ({ count: state.count + 1 })
}
})
So, I used JSX for "familiarity", but you don't need to use it to build your own apps. You can just write the code shown in the snippet above and you'd do just as well. You can also minimize and bundle your code to tap into the JavaScript ecosystem, code using a modular style and still not have to use JSX at all.