Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Created March 21, 2017 22:04
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 kristoferjoseph/17545c7cece149f0b9153755b4e68044 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/17545c7cece149f0b9153755b4e68044 to your computer and use it in GitHub Desktop.
Trite View component
var html = require('yo-yo')
module.exports = function View (store) {
var currentState = store.getState()
var subscribe = store.subscribe
var unsubscribe
var element
function load () {
unsubscribe = subscribe(update)
}
function unload () {
unsubscribe(update)
}
function render (state) {
currentState = state
return element = create(currentState)
}
function create (state) {
return html`
<div
onload=${load}
onunload=${unload}
>
${state}
</div>
`
}
function update (state) {
if (currentState === state) {
return
} else {
currentState = state
}
html.update(element, create(currentState))
}
return render(currentState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment