From 04c3e2e4077aa70fdb45a35957005a06498a42a9 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Wed, 20 Nov 2013 14:03:52 -0800 Subject: [PATCH] Add nextContext to componentWillReceiveProps Add `nextContext` as an argument to `componentWillReceiveProps`. We can figure out what to rename the method to later. --- src/core/ReactCompositeComponent.js | 12 ++++++------ src/core/__tests__/ReactCompositeComponent-test.js | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 541f01e13b..71eff83270 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -202,7 +202,7 @@ var ReactCompositeComponentInterface = { * Use this as an opportunity to react to a prop transition by updating the * state using `this.setState`. Current props are accessed via `this.props`. * - * componentWillReceiveProps: function(nextProps) { + * componentWillReceiveProps: function(nextProps, nextContext) { * this.setState({ * likesIncreasing: nextProps.likeCount > this.props.likeCount * }); @@ -823,6 +823,10 @@ var ReactCompositeComponentMixin = { return; } + var nextFullContext = this._pendingContext || this._currentContext; + var nextContext = this._processContext(nextFullContext); + this._pendingContext = null; + var nextProps = this.props; if (this._pendingProps != null) { nextProps = this._pendingProps; @@ -831,7 +835,7 @@ var ReactCompositeComponentMixin = { this._compositeLifeCycleState = CompositeLifeCycle.RECEIVING_PROPS; if (this.componentWillReceiveProps) { - this.componentWillReceiveProps(nextProps); + this.componentWillReceiveProps(nextProps, nextContext); } } @@ -840,10 +844,6 @@ var ReactCompositeComponentMixin = { var nextState = this._pendingState || this.state; this._pendingState = null; - var nextFullContext = this._pendingContext || this._currentContext; - var nextContext = this._processContext(nextFullContext); - this._pendingContext = null; - if (this._pendingForceUpdate || !this.shouldComponentUpdate || this.shouldComponentUpdate(nextProps, nextState, nextContext)) { diff --git a/src/core/__tests__/ReactCompositeComponent-test.js b/src/core/__tests__/ReactCompositeComponent-test.js index 7f2af7df83..188f618b9e 100644 --- a/src/core/__tests__/ReactCompositeComponent-test.js +++ b/src/core/__tests__/ReactCompositeComponent-test.js @@ -639,6 +639,7 @@ describe('ReactCompositeComponent', function() { }); it('should filter context properly in callbacks', function() { + var actualComponentWillReceiveProps; var actualShouldComponentUpdate; var actualComponentWillUpdate; var actualComponentDidUpdate; @@ -666,6 +667,11 @@ describe('ReactCompositeComponent', function() { foo: ReactPropTypes.string }, + componentWillReceiveProps: function(nextProps, nextContext) { + actualComponentWillReceiveProps = nextContext; + return true; + }, + shouldComponentUpdate: function(nextProps, nextState, nextContext) { actualShouldComponentUpdate = nextContext; return true; @@ -687,6 +693,7 @@ describe('ReactCompositeComponent', function() { var instance = ; ReactTestUtils.renderIntoDocument(instance); instance.replaceProps({foo: "def"}); + expect(actualComponentWillReceiveProps).toEqual({foo: 'def'}); expect(actualShouldComponentUpdate).toEqual({foo: 'def'}); expect(actualComponentWillUpdate).toEqual({foo: 'def'}); expect(actualComponentDidUpdate).toEqual({foo: 'abc'});