Skip to content

Commit

Permalink
Merge pull request #7 from AnSavvides/6-old-state-to-state-changed
Browse files Browse the repository at this point in the history
feat: pass old state to stateChanged
  • Loading branch information
tgvashworth committed Apr 18, 2016
2 parents bf23c00 + f843e5e commit 4ff4f53
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 4ff4f53

Please sign in to comment.