Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Last active October 26, 2017 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pyrolistical/882622fe00822418783dc63edcf04bf6 to your computer and use it in GitHub Desktop.
Save Pyrolistical/882622fe00822418783dc63edcf04bf6 to your computer and use it in GitHub Desktop.
hyperapp module state bug
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hyperapp Views</title>
</head>
<body>
<div id="app-entry"></div>
<script src="https://unpkg.com/hyperapp@0.15.1/dist/hyperapp.js"></script>
<script>
const { h, app } = hyperapp
function Counter({count, down, up}) {
return h("div", {}, [
h("h2", {}, count),
h("button", {
onclick: down,
disabled: count <= 0
}, "–"),
h("button", {
onclick: up
}, "+")
])
}
const counter = {
state: {
count: 0
},
actions: {
down(state) {
return {
count: state.count - 1
}
},
up(state) {
// we never get the new state
return {
count: state.count + 1
}
}
}
}
app({
view(state, actions) {
return h("main", {}, [
h(Counter, {
...state.counter,
...actions.counter
}),
h("button", {
onclick: actions.doubleUsingGlobal
}, "Double using global action"),
// this doesn't work
h("button", {
onclick: actions.counter.doubleUsingFragment
}, "Double using fragment action")
])
},
actions: {
doubleUsingGlobal(state, actions) {
return {
counter: {
count: 2 * state.counter.count
}
}
},
// this doesn't work due to module vs fragment conflict
counter: {
doubleUsingFragment(state, actions) {
return {
count: 2 * state.count
}
}
}
},
modules: {
counter
}
}, document.getElementById('app-entry'))
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment