Created
March 21, 2017 22:04
-
-
Save kristoferjoseph/17545c7cece149f0b9153755b4e68044 to your computer and use it in GitHub Desktop.
Trite View component
This file contains hidden or 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
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