Skip to content

Commit 0185c68

Browse files
committed
Merge pull request #3615 from jsfb/enable-new-context
Switch to parent-based context. Fixes #2112.
2 parents 0f0f5aa + 7d44917 commit 0185c68

File tree

5 files changed

+36
-287
lines changed

5 files changed

+36
-287
lines changed

src/classic/element/ReactElement.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ var ReactElement = function(type, key, ref, owner, context, props) {
9999
// Record the component responsible for creating this element.
100100
this._owner = owner;
101101

102-
// TODO: Deprecate withContext, and then the context becomes accessible
103-
// through the owner.
104-
this._context = context;
105-
106102
if (__DEV__) {
107103
// The validation flag and props are currently mutative. We put them on
108104
// an external backing store so that we can freeze the whole object.

src/classic/element/__tests__/ReactElement-test.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,6 @@ describe('ReactElement', function() {
8686
expect(element.props).toEqual({foo:'56'});
8787
});
8888

89-
it('preserves the legacy context on the element', function() {
90-
var Component = React.createFactory(ComponentClass);
91-
var element;
92-
93-
var Wrapper = React.createClass({
94-
childContextTypes: {
95-
foo: React.PropTypes.string
96-
},
97-
getChildContext: function() {
98-
return {foo: 'bar'};
99-
},
100-
render: function() {
101-
element = Component();
102-
return element;
103-
}
104-
});
105-
106-
ReactTestUtils.renderIntoDocument(
107-
React.createElement(Wrapper)
108-
);
109-
110-
expect(element._context).toEqual({foo: 'bar'});
111-
});
112-
11389
it('preserves the owner on the element', function() {
11490
var Component = React.createFactory(ComponentClass);
11591
var element;

src/core/ReactCompositeComponent.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ var ReactCompositeComponentMixin = {
125125
this._rootNodeID = rootID;
126126

127127
var publicProps = this._processProps(this._currentElement.props);
128-
var publicContext = this._processContext(this._currentElement._context);
128+
var publicContext = this._processContext(context);
129129

130130
var Component = ReactNativeComponent.getComponentClassForElement(
131131
this._currentElement
@@ -158,10 +158,6 @@ var ReactCompositeComponentMixin = {
158158
// Store a reference from the instance back to the internal representation
159159
ReactInstanceMap.set(inst, this);
160160

161-
if (__DEV__) {
162-
this._warnIfContextsDiffer(this._currentElement._context, context);
163-
}
164-
165161
if (__DEV__) {
166162
// Since plain JS classes are defined without any special initialization
167163
// logic, we can not catch common errors early. Therefore, we have to
@@ -529,30 +525,6 @@ var ReactCompositeComponentMixin = {
529525
}
530526
},
531527

532-
/**
533-
* Compare two contexts, warning if they are different
534-
* TODO: Remove this check when owner-context is removed
535-
*/
536-
_warnIfContextsDiffer: function(ownerBasedContext, parentBasedContext) {
537-
ownerBasedContext = this._maskContext(ownerBasedContext);
538-
parentBasedContext = this._maskContext(parentBasedContext);
539-
var parentKeys = Object.keys(parentBasedContext).sort();
540-
var displayName = this.getName() || 'ReactCompositeComponent';
541-
for (var i = 0; i < parentKeys.length; i++) {
542-
var key = parentKeys[i];
543-
warning(
544-
ownerBasedContext[key] === parentBasedContext[key],
545-
'owner-based and parent-based contexts differ ' +
546-
'(values: `%s` vs `%s`) for key (%s) while mounting %s ' +
547-
'(see: http://fb.me/react-context-by-parent)',
548-
ownerBasedContext[key],
549-
parentBasedContext[key],
550-
key,
551-
displayName
552-
);
553-
}
554-
},
555-
556528
/**
557529
* Perform an update to a mounted component. The componentWillReceiveProps and
558530
* shouldComponentUpdate methods are called, then (assuming the update isn't
@@ -582,18 +554,9 @@ var ReactCompositeComponentMixin = {
582554

583555
// Distinguish between a props update versus a simple state update
584556
if (prevParentElement !== nextParentElement) {
585-
nextContext = this._processContext(nextParentElement._context);
557+
nextContext = this._processContext(nextUnmaskedContext);
586558
nextProps = this._processProps(nextParentElement.props);
587559

588-
if (__DEV__) {
589-
if (nextUnmaskedContext != null) {
590-
this._warnIfContextsDiffer(
591-
nextParentElement._context,
592-
nextUnmaskedContext
593-
);
594-
}
595-
}
596-
597560
// An update here will schedule an update but immediately set
598561
// _pendingStateQueue which will ensure that any state updates gets
599562
// immediately reconciled instead of waiting for the next batch.

src/core/ReactContext.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111

1212
'use strict';
1313

14-
var assign = require('Object.assign');
1514
var emptyObject = require('emptyObject');
16-
var warning = require('warning');
17-
18-
var didWarn = false;
1915

2016
/**
2117
* Keeps track of the current context.
@@ -29,45 +25,7 @@ var ReactContext = {
2925
* @internal
3026
* @type {object}
3127
*/
32-
current: emptyObject,
33-
34-
/**
35-
* Temporarily extends the current context while executing scopedCallback.
36-
*
37-
* A typical use case might look like
38-
*
39-
* render: function() {
40-
* var children = ReactContext.withContext({foo: 'foo'}, () => (
41-
*
42-
* ));
43-
* return <div>{children}</div>;
44-
* }
45-
*
46-
* @param {object} newContext New context to merge into the existing context
47-
* @param {function} scopedCallback Callback to run with the new context
48-
* @return {ReactComponent|array<ReactComponent>}
49-
*/
50-
withContext: function(newContext, scopedCallback) {
51-
if (__DEV__) {
52-
warning(
53-
didWarn,
54-
'withContext is deprecated and will be removed in a future version. ' +
55-
'Use a wrapper component with getChildContext instead.'
56-
);
57-
58-
didWarn = true;
59-
}
60-
61-
var result;
62-
var previousContext = ReactContext.current;
63-
ReactContext.current = assign({}, previousContext, newContext);
64-
try {
65-
result = scopedCallback();
66-
} finally {
67-
ReactContext.current = previousContext;
68-
}
69-
return result;
70-
}
28+
current: emptyObject
7129

7230
};
7331

0 commit comments

Comments
 (0)