From 8a460ba2e83ce47fa0198865d4bdf50617306971 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Sun, 25 May 2014 00:40:39 -0700 Subject: [PATCH] Auto-bind before getDefaultProps Fixes #1595. --- src/core/ReactCompositeComponent.js | 8 +++---- .../__tests__/ReactCompositeComponent-test.js | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index daf90b59ed..d31b5c9fb4 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -724,14 +724,14 @@ var ReactCompositeComponentMixin = { ); this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING; - this.context = this._processContext(this._descriptor._context); - this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null; - this.props = this._processProps(this.props); - if (this.__reactAutoBindMap) { this._bindAutoBindMethods(); } + this.context = this._processContext(this._descriptor._context); + this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null; + this.props = this._processProps(this.props); + this.state = this.getInitialState ? this.getInitialState() : null; invariant( typeof this.state === 'object' && !Array.isArray(this.state), diff --git a/src/core/__tests__/ReactCompositeComponent-test.js b/src/core/__tests__/ReactCompositeComponent-test.js index e3a9d2af52..5e0dbe71b5 100644 --- a/src/core/__tests__/ReactCompositeComponent-test.js +++ b/src/core/__tests__/ReactCompositeComponent-test.js @@ -376,6 +376,29 @@ describe('ReactCompositeComponent', function() { }); + it('should auto bind before getDefaultProps', function() { + var calls = 0; + var Component = React.createClass({ + getDefaultProps: function() { + return { + onClick: this.defaultClickHandler + }; + }, + defaultClickHandler: function() { + expect(this).toBe(instance); + calls++; + }, + render: function() { + return
; + } + }); + var instance = ReactTestUtils.renderIntoDocument(); + var handler = instance.props.onClick; + // Call handler with no context + handler(); + expect(calls).toBe(1); + }); + it('should use default values for undefined props', function() { var Component = React.createClass({ getDefaultProps: function() {