Created
March 2, 2016 17:39
-
-
Save freeman-lab/758a89c9ec5b42e93d94 to your computer and use it in GitHub Desktop.
redux + hyperx + main-loop
This file contains 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 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) |
This file contains 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
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