Auto-bind before getDefaultProps
Fixes #1595.
Ben Alpert committed
May 25, 2014 at 00:40 UTC
8a460ba2e83ce47fa0198865d4bdf50617306971
2 files changed
+27
-4
src/core/ReactCompositeComponent.js
+4
-4
@@ -724,14 +724,14 @@ var ReactCompositeComponentMixin = {
724
);
725
this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;
726
727
- this.context = this._processContext(this._descriptor._context);
728
- this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null;
729
- this.props = this._processProps(this.props);
730
-
727
if (this.__reactAutoBindMap) {
728
this._bindAutoBindMethods();
729
}
730
731
+ this.context = this._processContext(this._descriptor._context);
732
+ this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null;
733
+ this.props = this._processProps(this.props);
734
+
735
this.state = this.getInitialState ? this.getInitialState() : null;
736
invariant(
737
typeof this.state === 'object' && !Array.isArray(this.state),
src/core/__tests__/ReactCompositeComponent-test.js
+23
@@ -376,6 +376,29 @@ describe('ReactCompositeComponent', function() {
376
377
});
378
379
+ it('should auto bind before getDefaultProps', function() {
380
+ var calls = 0;
381
+ var Component = React.createClass({
382
+ getDefaultProps: function() {
383
+ return {
384
+ onClick: this.defaultClickHandler
385
+ };
386
+ },
387
+ defaultClickHandler: function() {
388
+ expect(this).toBe(instance);
389
+ calls++;
390
+ },
391
+ render: function() {
392
+ return <div onClick={this.props.onClick}></div>;
393
+ }
394
+ });
395
+ var instance = ReactTestUtils.renderIntoDocument(<Component />);
396
+ var handler = instance.props.onClick;
397
+ // Call handler with no context
398
+ handler();
399
+ expect(calls).toBe(1);
400
+ });
401
+
402
it('should use default values for undefined props', function() {
403
var Component = React.createClass({
404
getDefaultProps: function() {