Skip to content

Instantly share code, notes, and snippets.

@Swizz
Last active June 30, 2017 16:14
Show Gist options
  • Save Swizz/8437df6ac5ec4985e72e648d242f6a9f to your computer and use it in GitHub Desktop.
Save Swizz/8437df6ac5ec4985e72e648d242f6a9f to your computer and use it in GitHub Desktop.
hyperapp component proposal
export default (props = { initialValue: 0, unit: '' }, children) => ({
state: {
count: props.initialValue,
unit: props.unit
},
actions: {
inc: (state, actions, data, emit) => emit('change', { count: state.count + 1 }),
dec: (state, actions, data, emit) => emit('change', { count: state.count + 1 })
},
view: (state, actions) => (
<div class={"counter"}>
<button onclick={actions.inc}> + </button>
<span>{state.count} {state.unit}</span>
<button onclick={actions.dec}> - </button>
</div>
)
})
import { h, app } from "hyperapp"
import Counter from "./counter"
app({
state: {
title: 'Best counter ever',
unit: 'monkeys'
},
view: (state, actions, { Counter }, emit) => (
<main>
<h1>{state.title}</h1>
<Counter/>
</main>
),
components: state => ({
Counter: (
<Counter initialValue={5}, unit={state.unit}, onchange={value => emit('change', value)}/>
)
}),
events: {
change: (state, actions, value) => { alert(value) }
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment