From d7d3ea55608e53a19edf53172274d16014fdd021 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 15 Jan 2015 14:00:45 -0800 Subject: [PATCH] Don't bind callbacks to setState, setProps etc. We added a bind to the public instance, but instead we can just pass the public instance as the context when we execute. This avoids a warning that fires when we call bind on auto-bound methods. --- src/classic/class/ReactClass.js | 6 +-- src/classic/class/__tests__/ReactBind-test.js | 42 +++++++++++++++++++ src/core/ReactUpdates.js | 2 +- src/modern/class/ReactComponentBase.js | 4 +- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/classic/class/ReactClass.js b/src/classic/class/ReactClass.js index 7382bc9f46..fa896fc722 100644 --- a/src/classic/class/ReactClass.js +++ b/src/classic/class/ReactClass.js @@ -712,7 +712,7 @@ var ReactClassMixin = { ); internalInstance.replaceState( newState, - callback && callback.bind(this) + callback ); }, @@ -746,7 +746,7 @@ var ReactClassMixin = { ); internalInstance.setProps( partialProps, - callback && callback.bind(this) + callback ); }, @@ -762,7 +762,7 @@ var ReactClassMixin = { replaceProps: function(newProps, callback) { ReactInstanceMap.get(this).replaceProps( newProps, - callback && callback.bind(this) + callback ); } }; diff --git a/src/classic/class/__tests__/ReactBind-test.js b/src/classic/class/__tests__/ReactBind-test.js index 4f9acb2141..f2a5885a29 100644 --- a/src/classic/class/__tests__/ReactBind-test.js +++ b/src/classic/class/__tests__/ReactBind-test.js @@ -124,4 +124,46 @@ describe('autobinding', function() { expect(mouseDidClick.mock.instances[0]).toBe(mountedInstance1); }); + it('warns if you try to bind to this', function() { + spyOn(console, 'warn'); + + var TestBindComponent = React.createClass({ + handleClick: function() { }, + render: function() { + return
; + } + }); + + ReactTestUtils.renderIntoDocument() + + expect(console.warn.argsForCall.length).toBe(1); + expect(console.warn.argsForCall[0][0]).toContain( + 'You are binding a component method to the component. ' + + 'React does this for you automatically' + ); + }); + + it('does not warn if you pass an auto-bound method to setState', function() { + spyOn(console, 'warn'); + + var TestBindComponent = React.createClass({ + getInitialState: function() { + return { foo: 1 }; + }, + componentDidMount: function() { + this.setState({ foo: 2 }, this.handleUpdate); + }, + handleUpdate: function() { + + }, + render: function() { + return
; + } + }); + + ReactTestUtils.renderIntoDocument() + + expect(console.warn.argsForCall.length).toBe(0); + }); + }); diff --git a/src/core/ReactUpdates.js b/src/core/ReactUpdates.js index 3bbb40215f..7323715d82 100644 --- a/src/core/ReactUpdates.js +++ b/src/core/ReactUpdates.js @@ -152,7 +152,7 @@ function runBatchedUpdates(transaction) { for (var j = 0; j < callbacks.length; j++) { transaction.callbackQueue.enqueue( callbacks[j], - component + component.getPublicInstance() ); } } diff --git a/src/modern/class/ReactComponentBase.js b/src/modern/class/ReactComponentBase.js index 68a60bfc78..a3308f64ff 100644 --- a/src/modern/class/ReactComponentBase.js +++ b/src/modern/class/ReactComponentBase.js @@ -61,7 +61,7 @@ ReactComponentBase.prototype.setState = function(partialState, callback) { 'component.' ); internalInstance.setState( - partialState, callback && callback.bind(this) + partialState, callback ); }; @@ -87,7 +87,7 @@ ReactComponentBase.prototype.forceUpdate = function(callback) { 'components. This usually means you called forceUpdate() on an ' + 'unmounted component.' ); - internalInstance.forceUpdate(callback && callback.bind(this)); + internalInstance.forceUpdate(callback); }; /**