Merge pull request #1597 from spicyj/defaultprops-autobind
Auto-bind before getDefaultProps
This commit is contained in:
commit
8e2dcceee3
|
@ -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),
|
||||
|
|
|
@ -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 <div onClick={this.props.onClick}></div>;
|
||||
}
|
||||
});
|
||||
var instance = ReactTestUtils.renderIntoDocument(<Component />);
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue