Skip to content

Instantly share code, notes, and snippets.

@freeman-lab
Created March 2, 2016 17:39
Show Gist options
  • Save freeman-lab/758a89c9ec5b42e93d94 to your computer and use it in GitHub Desktop.
Save freeman-lab/758a89c9ec5b42e93d94 to your computer and use it in GitHub Desktop.
redux + hyperx + main-loop
var redux = require('redux')
var vdom = require('virtual-dom')
var hyperx = require('hyperx')
var hx = hyperx(vdom.h)
var reducer = require('./reducer')
var store = redux.createStore(reducer)
var item = function (state) {
function onclick () {
store.dispatch({ type: 'INCREMENT'})
}
return hx`<div><span>${state}</span><button onclick=${onclick}></button></div>`
}
var list = function (state) {
return hx`<div>${state.value.map(function (d) {return item(d)})}</div>`
}
var main = require('main-loop')
var loop = main({value: [0]}, render, vdom)
document.body.appendChild(loop.target)
function render (state) {
return list(state)
}
function update () {
loop.update(store.getState())
}
store.subscribe(update)
module.exports = function (state, action) {
if (typeof state === 'undefined') state = {value: [0]}
console.log(state)
console.log(action)
switch (action.type) {
case 'INCREMENT':
return Object.assign(state, {value: state.value.concat([1])})
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment