Add nextContext to componentWillReceiveProps
Add `nextContext` as an argument to `componentWillReceiveProps`. We can figure out what to rename the method to later.
Tim Yung committed
Nov 20, 2013 at 14:03 UTC
04c3e2e4077aa70fdb45a35957005a06498a42a9
2 files changed
+13
-6
src/core/ReactCompositeComponent.js
+6
-6
@@ -202,7 +202,7 @@ var ReactCompositeComponentInterface = {
202
* Use this as an opportunity to react to a prop transition by updating the
203
* state using `this.setState`. Current props are accessed via `this.props`.
204
*
205
- * componentWillReceiveProps: function(nextProps) {
205
+ * componentWillReceiveProps: function(nextProps, nextContext) {
206
* this.setState({
207
* likesIncreasing: nextProps.likeCount > this.props.likeCount
208
* });
@@ -823,6 +823,10 @@ var ReactCompositeComponentMixin = {
823
return;
824
}
825
826
+ var nextFullContext = this._pendingContext || this._currentContext;
827
+ var nextContext = this._processContext(nextFullContext);
828
+ this._pendingContext = null;
829
+
830
var nextProps = this.props;
831
if (this._pendingProps != null) {
832
nextProps = this._pendingProps;
@@ -831,7 +835,7 @@ var ReactCompositeComponentMixin = {
835
836
this._compositeLifeCycleState = CompositeLifeCycle.RECEIVING_PROPS;
837
if (this.componentWillReceiveProps) {
834
- this.componentWillReceiveProps(nextProps);
838
+ this.componentWillReceiveProps(nextProps, nextContext);
839
}
840
}
841
@@ -840,10 +844,6 @@ var ReactCompositeComponentMixin = {
844
var nextState = this._pendingState || this.state;
845
this._pendingState = null;
846
843
- var nextFullContext = this._pendingContext || this._currentContext;
844
- var nextContext = this._processContext(nextFullContext);
845
- this._pendingContext = null;
846
-
847
if (this._pendingForceUpdate ||
848
!this.shouldComponentUpdate ||
849
this.shouldComponentUpdate(nextProps, nextState, nextContext)) {
src/core/__tests__/ReactCompositeComponent-test.js
+7
@@ -639,6 +639,7 @@ describe('ReactCompositeComponent', function() {
639
});
640
641
it('should filter context properly in callbacks', function() {
642
+ var actualComponentWillReceiveProps;
643
var actualShouldComponentUpdate;
644
var actualComponentWillUpdate;
645
var actualComponentDidUpdate;
@@ -666,6 +667,11 @@ describe('ReactCompositeComponent', function() {
667
foo: ReactPropTypes.string
668
},
669
670
+ componentWillReceiveProps: function(nextProps, nextContext) {
671
+ actualComponentWillReceiveProps = nextContext;
672
+ return true;
673
+ },
674
+
675
shouldComponentUpdate: function(nextProps, nextState, nextContext) {
676
actualShouldComponentUpdate = nextContext;
677
return true;
@@ -687,6 +693,7 @@ describe('ReactCompositeComponent', function() {
693
var instance = <Parent foo="abc" />;
694
ReactTestUtils.renderIntoDocument(instance);
695
instance.replaceProps({foo: "def"});
696
+ expect(actualComponentWillReceiveProps).toEqual({foo: 'def'});
697
expect(actualShouldComponentUpdate).toEqual({foo: 'def'});
698
expect(actualComponentWillUpdate).toEqual({foo: 'def'});
699
expect(actualComponentDidUpdate).toEqual({foo: 'abc'});