Last active
June 19, 2018 07:30
-
-
Save Swiip/7d1f40dc97b766c11c2a0a3be02ec365 to your computer and use it in GitHub Desktop.
Compo counter 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, | |
css, | |
createStore, | |
component, | |
withProp, | |
withStore, | |
withStyle, | |
withMarkup, | |
} from 'compo-lib'; | |
createStore((state, action) => { | |
switch (action.type) { | |
case 'ADD': return state + 1; | |
case 'SUB': return state - 1; | |
default: return state; | |
} | |
}, 0); | |
component( | |
'my-counter-label', | |
withProp('value'), | |
withStyle(({ value }) => css` | |
:host { | |
color: ${value < 1 ? 'red' : 'black'} | |
} | |
`), | |
); | |
component( | |
'my-counter', | |
withStore(({ getState, dispatch }) => ({ | |
counter: getState(), | |
add: () => dispatch({ type: 'ADD' }), | |
sub: () => dispatch({ type: 'SUB' }), | |
})), | |
withMarkup(({ counter, add, sub }) => html` | |
<div> | |
<my-counter-label value=${counter}>${counter}</my-counter-label> | |
<button onclick=${add}>+</button> | |
<button onclick=${sub}>-</button> | |
</div> | |
`), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment