Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created January 17, 2017 00:26
Show Gist options
  • Save ishiduca/bdbf51ad84783256b428781643c8d2fa to your computer and use it in GitHub Desktop.
Save ishiduca/bdbf51ad84783256b428781643c8d2fa to your computer and use it in GitHub Desktop.
yo-yo with send-action
var d = require('global/document')
var onload = require('on-load')
var yo = require('yo-yo')
var sendAction = require('send-action')
var root = d.createElement('main')
var send = sendAction({
onAction (state, action, data) {
console.log('onAction')
console.log(state)
console.log(action)
console.log(data)
if (action === 'example') {
return {value: data}
}
return state
},
onChange (state, prev) {
console.log('onChnage')
console.log(state)
console.log(prev)
yo.update(root, render(state))
},
state: { value: 'ok' }
})
var render = state => yo `<main>${state.value}</main>`
onload(root, rt => yo.update(rt, render(send.state())))
d.body.appendChild(root)
setTimeout(() => send('example', 'hoe~'), 1000)
setTimeout(() => send('exile', 'EXILE'), 3000)
@ishiduca
Copy link
Author

onAction
{ value="ok"}
example
hoe~

onChnage
{ value="hoe~"}
{ value="ok"}

onAction
{ value="hoe~"}
exile
EXILE

onChnage
{ value="hoe~"}
{ value="hoe~"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment