Closed lijunle closed 9 years ago
For this line of code, it constructs the test code to the following object:
React.addons.update(state, {a:{$apply:8}})
Then exception!
Before the check in, it constructs as:
React.addons.update(state, {a:{$apply:function(x) { return x / 8 }}})
So, it seems that, the new updater function API originally can be implemented by $apply
function. If we want keep the new API, maybe it could be OK to remove the apply
delegation.
Besides, it seems a litter confusing for the following code:
cursor.refine('a').push(function (list) { return list.concat([4,5]) }); // somebody might want to push 4 and 5 to the end of the list, but...
And, one more thing... React do shadow copy to avoid the apply function modify the original object.
E.g, the following code still works fine: (JSFiddle)
cursor.refine('a').apply(function (obj) { obj.value = 10; return obj; }); // will the original obj modified?
How about this? (JSFiddle)
cursor.refine('a').apply(function (obj) { obj.nested.value = 10; return obj; }); // will the original obj modified?
The result seems interesting. :smile:
IMO, it may be better to revert the updater function and leave apply
function for users' needs. It has to say that, the apply
function is not working well. But, we could better to expose those limitations.
I fixed the test https://github.com/dustingetz/react-cursor/commit/d4491fb509a9648a1572a29b9a119e198bad9e5b#diff-f9b3da4de5b1f0fbdd8ab24842ce0412 so i could release this as the current code is stable and react 13 was basically broken until these commits. Leaving this issue open for further thought. I agree that it doesn't look like apply
is needed anymore.
@dustingetz The current update implementation will break the shouldComponentUpdate
helper with the following code:
cursor.refine('a').set(function (obj) {
obj.b = obj.b + 1;
return obj;
});
I write a failing test case to validate this.
If you do not think it is a bug, it could better to clarify it in README.
Yeah, object mutation is not allowed in react-cursor, will clarify the readme, thanks.
(Fork from #45 )
The update function is introduced in this commit 154b986.
The failing test case details: