Skip to content

Commit

Permalink
feat: pass old state to stateChanged
Browse files Browse the repository at this point in the history
Having access to both the current `state` and `previousState`
means you can add logic on state changes to decide whether
you need to act on them or not (e.g. re-rendering).

Closes #6.
  • Loading branch information
Andreas Savvides committed Apr 18, 2016
1 parent bf23c00 commit f843e5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ var ToggleButton = flight.component(
});
};

this.update = function () {
this.$node.toggleClass('is-active', this.state.active);
this.update = function (state, previousState) {
this.$node.toggleClass('is-active', state.active);
};
}
);
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ export default function withState() {
if (!state || typeof state !== 'object') {
return;
}

const oldState = this.state;

this.state = state;
this.stateChanged(this.state);
this.stateChanged(this.state, oldState);
return this.state;
};

Expand Down Expand Up @@ -139,8 +142,13 @@ export default function withState() {

/**
* Noop for advice around state changes.
*
* Having access to both the current `state`
* and `previousState` means you can add logic
* on state changes to decide whether you need
* to act on them or not (e.g. re-rendering).
*/
this.stateChanged = function () {};
this.stateChanged = function (state, previousState) {};

this.after('initialize', function () {
this._stateDef = (this._stateDef || noop);
Expand Down

0 comments on commit f843e5e

Please sign in to comment.