Skip to content
This repository has been archived by the owner on Oct 14, 2018. It is now read-only.

Commit

Permalink
Cursor.set's nextValue parameter now expects a function
Browse files Browse the repository at this point in the history
It remains backwards compatible with usages that pass non-functions,
however.
  • Loading branch information
danielmiladinov committed Jul 23, 2015
1 parent 01ac5de commit 154b986
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/helloworld/webapp/js/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ var ClickerDescriptor = React.createClass({
},

inc2: function () {
this.props.cursor.set(this.props.cursor.pendingValue() + 1);
this.props.cursor.set(this.props.cursor.pendingValue() + 1);
this.props.cursor.set(function (s) { return s + 1; });
this.props.cursor.set(function (s) { return s + 1; });
},

inc10: function () {
Expand Down
21 changes: 17 additions & 4 deletions src/Cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@ function Cursor(cmp, path, value) {
};
}

function update(cmp, path, operation, nextValue) {
function update(cmp, path, operation, nextUpdate) {

This comment has been minimized.

Copy link
@lijunle

lijunle Jul 23, 2015

Contributor

Some unit test for the update scenario is great.

// Backwards compatibility with non-function values of nextUpdate
if (typeof nextUpdate !== "function") {
var prevValue = nextUpdate;
nextUpdate = function ( ) { return prevValue; };
}

cmp.setState(function (state) {
var nextState;

if (path.length > 0) {
return React.addons.update(
nextState = React.addons.update(
state,
path.concat(operation).reduceRight(util.unDeref, nextValue)
path.concat(operation).reduceRight(
util.unDeref,
nextUpdate(util.getRefAtPath(state, path))
)
);
} else if (path.length === 0) {
return nextValue;
nextState = nextUpdate(state);
}

return nextState;
});
}

Expand Down

1 comment on commit 154b986

@lijunle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dustingetz you could still needs to update the _pendingState call, which is removed as of 0.13-rc1.

There is a workaround to make pendingValue API back-compatible to React 0.13. But I don't know the real scenario for the pendingValue API. As as React blog said:

this.setState() can now take a function as the first argument for transactional state updates, such as this.setState((state, props) => ({count: state.count + 1})); -- this means that you no longer need to use this._pendingState, which is now gone.

Please sign in to comment.