Version 2.0.0 - October 25, 2015
This is the second major version for Hoverboard, and makes some major API changes and simplifications.
Hoverboard now only takes an object of actions, with the following changes:
- Each action method is essentially a reducer with the signature:
function action (state, arg1, .., argN) { return newState }
- When you call the action, you call it with just the arguments, eg.
store.action(arg1, .., argN);
- If the action reducer returns an object, it will be shallow merged into the state if the state is also an object.
- The state passed to the action or to subscribers will be a shallow copy of the state, if it is an object, to prevent mutations.
- Action method names are unchanged, ie. Hoverboard no longer prepends "on" to actions.
There are some other things that are new:
- You can no longer pass a function to Hoverboard, nor return one from the actions.
- Anything async response needs to go through an action, there is no way to
setState
otherwise. - You can no longer use a
getInitialState
method. Instead, you'll need to create an action that sets the initial state and then trigger that action on the store before using the store. - There are no longer
this.getState
,this.setState
orthis.replaceState
methods. - You no longer have any need to use
this
with Hoverboard at all, so your actions can be pure functions. - Hoverboard is now only 555 bytes minimized and gzipped!
Here's an updated example:
var ClickCounter = Hoverboard({
click: function(state, text) {
return {
value: state.value + 1,
log: state.log.concat(text)
};
},
reset: function() {
// go back to defaults
return {
value: 0,
log: []
};
}
});
// initialize with defaults
ClickCounter.reset();
// listen to changes to the state
var unsubscribe = ClickCounter(function (clickState) {
document.write(JSON.stringify(clickState) + "<br>");
});
ClickCounter.click('first');
ClickCounter.click('second');
// reset back to empty state
ClickCounter.reset();
unsubscribe();
ClickCounter.click("This won't show up");
Check out the README for updated documentation and examples.