Last active
June 30, 2017 16:14
-
-
Save Swizz/8437df6ac5ec4985e72e648d242f6a9f to your computer and use it in GitHub Desktop.
hyperapp component proposal
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
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> | |
) | |
}) |
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 { 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