Created
March 6, 2018 09:01
-
-
Save radum/830ea74021716fbe8f227ef479dae0e8 to your computer and use it in GitHub Desktop.
developit/unistore Example
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
import createStore from "unistore"; | |
function mapActions(actions, store) { | |
if (typeof actions === "function") actions = actions(store); | |
let mapped = {}; | |
for (let i in actions) { | |
mapped[i] = store.action(actions[i]); | |
} | |
return mapped; | |
} | |
let store = createStore({ count: 0 }); | |
let actions = { | |
increment: state => { | |
return { count: state.count + 1 }; | |
} | |
}; | |
console.log(store.getState()); | |
var stateActions = mapActions(actions, store); | |
store.subscribe(function(state, action) { | |
var actionName = (action && action.name) || "setState"; | |
console.log("state: " + JSON.stringify(state)); | |
console.log("actionName: " + actionName); | |
}); | |
stateActions.increment(); | |
stateActions.increment(); | |
stateActions.increment(); | |
console.log(store.getState()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment