Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger componentWillReceiveProps when context changes #5787

Merged
merged 1 commit into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,26 +647,33 @@ var ReactCompositeComponentMixin = {
nextUnmaskedContext
) {
var inst = this._instance;

var nextContext = this._context === nextUnmaskedContext ?
inst.context :
this._processContext(nextUnmaskedContext);
var willReceive = false;
var nextContext;
var nextProps;

// Determine if the context has changed or not
if (this._context === nextUnmaskedContext) {
nextContext = inst.context;
} else {
nextContext = this._processContext(nextUnmaskedContext);
willReceive = true;
}

// Distinguish between a props update versus a simple state update
if (prevParentElement === nextParentElement) {
// Skip checking prop types again -- we don't read inst.props to avoid
// warning for DOM component props in this upgrade
nextProps = nextParentElement.props;
} else {
nextProps = this._processProps(nextParentElement.props);
// An update here will schedule an update but immediately set
// _pendingStateQueue which will ensure that any state updates gets
// immediately reconciled instead of waiting for the next batch.
willReceive = true;
}

if (inst.componentWillReceiveProps) {
inst.componentWillReceiveProps(nextProps, nextContext);
}
// An update here will schedule an update but immediately set
// _pendingStateQueue which will ensure that any state updates gets
// immediately reconciled instead of waiting for the next batch.
if (willReceive && inst.componentWillReceiveProps) {
inst.componentWillReceiveProps(nextProps, nextContext);
}

var nextState = this._processPendingState(nextProps, nextContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,123 @@ describe('ReactCompositeComponent', function() {
expect(div.children[0].id).toBe('aliens');
});

it('should trigger componentWillReceiveProps for context changes', function() {
var contextChanges = 0;
var propChanges = 0;

var GrandChild = React.createClass({
contextTypes: {
foo: ReactPropTypes.string.isRequired,
},

componentWillReceiveProps: function(nextProps, nextContext) {
expect('foo' in nextContext).toBe(true);

if (nextProps !== this.props) {
propChanges++;
}

if (nextContext !== this.context) {
contextChanges++;
}
},

render: function() {
return <span className="grand-child">{this.props.children}</span>;
},
});

var ChildWithContext = React.createClass({
contextTypes: {
foo: ReactPropTypes.string.isRequired,
},

componentWillReceiveProps: function(nextProps, nextContext) {
expect('foo' in nextContext).toBe(true);

if (nextProps !== this.props) {
propChanges++;
}

if (nextContext !== this.context) {
contextChanges++;
}
},

render: function() {
return <div className="child-with">{this.props.children}</div>;
},
});

var ChildWithoutContext = React.createClass({
componentWillReceiveProps: function(nextProps, nextContext) {
expect('foo' in nextContext).toBe(false);

if (nextProps !== this.props) {
propChanges++;
}

if (nextContext !== this.context) {
contextChanges++;
}
},

render: function() {
return <div className="child-without">{this.props.children}</div>;
},
});

var Parent = React.createClass({
childContextTypes: {
foo: ReactPropTypes.string,
},

getInitialState() {
return {
foo: 'abc',
};
},

getChildContext: function() {
return {
foo: this.state.foo,
};
},

onClick() {
this.setState({
foo: 'def',
});
},

render: function() {
return <div className="parent" onClick={this.onClick}>{this.props.children}</div>;
},
});

var div = document.createElement('div');

ReactDOM.render(
<Parent>
<ChildWithoutContext>
A1
<GrandChild>A2</GrandChild>
</ChildWithoutContext>

<ChildWithContext>
B1
<GrandChild>B2</GrandChild>
</ChildWithContext>
</Parent>,
div
);

ReactTestUtils.Simulate.click(div.childNodes[0]);

expect(propChanges).toBe(0);
expect(contextChanges).toBe(3); // ChildWithContext, GrandChild x 2
});

it('should disallow nested render calls', function() {
var Inner = React.createClass({
render: function() {
Expand Down