Skip to content

Commit 8aa6171

Browse files
committed
Eliminate context warnings when component isn't reading the conflicting context variable.
1 parent fa40eef commit 8aa6171

2 files changed

Lines changed: 36 additions & 53 deletions

File tree

src/core/ReactCompositeComponent.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,13 @@ var ReactCompositeComponentMixin = assign({},
457457

458458
/**
459459
* Filters the context object to only contain keys specified in
460-
* `contextTypes`, and asserts that they are valid.
460+
* `contextTypes`
461461
*
462462
* @param {object} context
463463
* @return {?object}
464464
* @private
465465
*/
466-
_processContext: function(context) {
466+
_maskContext: function(context) {
467467
var maskedContext = null;
468468
var contextTypes = this._instance.constructor.contextTypes;
469469
if (!contextTypes) {
@@ -473,6 +473,23 @@ var ReactCompositeComponentMixin = assign({},
473473
for (var contextName in contextTypes) {
474474
maskedContext[contextName] = context[contextName];
475475
}
476+
return maskedContext;
477+
},
478+
479+
/**
480+
* Filters the context object to only contain keys specified in
481+
* `contextTypes`, and asserts that they are valid.
482+
*
483+
* @param {object} context
484+
* @return {?object}
485+
* @private
486+
*/
487+
_processContext: function(context) {
488+
var maskedContext = this._maskContext(context);
489+
var contextTypes = this._instance.constructor.contextTypes;
490+
if (!contextTypes) {
491+
return emptyObject;
492+
}
476493
if (__DEV__) {
477494
this._checkPropTypes(
478495
contextTypes,
@@ -648,35 +665,23 @@ var ReactCompositeComponentMixin = assign({},
648665
* TODO: Remove this check when owner-context is removed
649666
*/
650667
_warnIfContextsDiffer: function(ownerBasedContext, parentBasedContext) {
668+
ownerBasedContext = this._maskContext(ownerBasedContext);
669+
parentBasedContext = this._maskContext(parentBasedContext);
651670
var ownerKeys = Object.keys(ownerBasedContext).sort();
652671
var parentKeys = Object.keys(parentBasedContext).sort();
653672
var displayName = this.getName() || 'ReactCompositeComponent';
654-
if (ownerKeys.length !== parentKeys.length ||
655-
ownerKeys.toString() !== parentKeys.toString()) {
673+
for (var i = 0; i < parentKeys.length; i++) {
674+
var key = parentKeys[i];
656675
warning(
657-
ownerKeys.length === parentKeys.length &&
658-
ownerKeys.toString() === parentKeys.toString(),
659-
'owner based context (keys: %s) does not equal parent based ' +
660-
'context (keys: %s) while mounting %s ' +
676+
ownerBasedContext[key] === parentBasedContext[key],
677+
'owner-based and parent-based contexts differ ' +
678+
'(values: `%s` vs `%s`) for key (%s) while mounting %s ' +
661679
'(see: http://fb.me/react-context-by-parent)',
662-
Object.keys(ownerBasedContext),
663-
Object.keys(parentBasedContext),
680+
ownerBasedContext[key],
681+
parentBasedContext[key],
682+
key,
664683
displayName
665684
);
666-
} else {
667-
for (var i = 0; i < parentKeys.length; i++) {
668-
var key = parentKeys[i];
669-
warning(
670-
ownerBasedContext[key] === parentBasedContext[key],
671-
'owner-based and parent-based contexts differ ' +
672-
'(values: `%s` vs `%s`) for key (%s) while mounting %s ' +
673-
'(see: http://fb.me/react-context-by-parent)',
674-
ownerBasedContext[key],
675-
parentBasedContext[key],
676-
key,
677-
displayName
678-
);
679-
}
680685
}
681686
},
682687

src/core/__tests__/ReactCompositeComponent-test.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -581,16 +581,11 @@ describe('ReactCompositeComponent', function() {
581581

582582
// Two warnings, one for the component and one for the div
583583
// We may want to make this expect one warning in the future
584-
expect(console.warn.mock.calls.length).toBe(2);
584+
expect(console.warn.mock.calls.length).toBe(1);
585585
expect(console.warn.mock.calls[0][0]).toBe(
586-
'Warning: owner based context (keys: foo) does not equal parent based ' +
587-
'context (keys: ) while mounting Component ' +
588-
'(see: http://fb.me/react-context-by-parent)'
589-
);
590-
expect(console.warn.mock.calls[1][0]).toBe(
591-
'Warning: owner based context (keys: foo) does not equal parent based ' +
592-
'context (keys: ) while mounting ReactCompositeComponent ' +
593-
'(see: http://fb.me/react-context-by-parent)'
586+
'Warning: owner-based and parent-based contexts differ '+
587+
'(values: `bar` vs `undefined`) for key (foo) '+
588+
'while mounting Component (see: http://fb.me/react-context-by-parent)'
594589
);
595590

596591
});
@@ -629,17 +624,12 @@ describe('ReactCompositeComponent', function() {
629624

630625
// Two warnings, one for the component and one for the div
631626
// We may want to make this expect one warning in the future
632-
expect(console.warn.mock.calls.length).toBe(2);
627+
expect(console.warn.mock.calls.length).toBe(1);
633628
expect(console.warn.mock.calls[0][0]).toBe(
634629
'Warning: owner-based and parent-based contexts differ ' +
635630
'(values: `noise` vs `bar`) for key (foo) while mounting Component ' +
636631
'(see: http://fb.me/react-context-by-parent)'
637632
);
638-
expect(console.warn.mock.calls[1][0]).toBe(
639-
'Warning: owner-based and parent-based contexts differ ' +
640-
'(values: `noise` vs `bar`) for key (foo) while mounting ReactCompositeComponent ' +
641-
'(see: http://fb.me/react-context-by-parent)'
642-
);
643633

644634
});
645635

@@ -686,18 +676,12 @@ describe('ReactCompositeComponent', function() {
686676

687677
// Two warnings, one for the component and one for the div
688678
// We may want to make this expect one warning in the future
689-
expect(console.warn.mock.calls.length).toBe(2);
679+
expect(console.warn.mock.calls.length).toBe(1);
690680
expect(console.warn.mock.calls[0][0]).toBe(
691681
'Warning: owner-based and parent-based contexts differ ' +
692682
'(values: `noise` vs `bar`) for key (foo) while mounting Component ' +
693683
'(see: http://fb.me/react-context-by-parent)'
694684
);
695-
expect(console.warn.mock.calls[1][0]).toBe(
696-
'Warning: owner-based and parent-based contexts differ ' +
697-
'(values: `noise` vs `bar`) for key (foo) while mounting ReactCompositeComponent ' +
698-
'(see: http://fb.me/react-context-by-parent)'
699-
);
700-
701685
});
702686

703687
it('should warn if context values differ on update using wrapper', function() {
@@ -746,18 +730,12 @@ describe('ReactCompositeComponent', function() {
746730

747731
// Two warnings, one for the component and one for the div
748732
// We may want to make this expect one warning in the future
749-
expect(console.warn.mock.calls.length).toBe(2);
733+
expect(console.warn.mock.calls.length).toBe(1);
750734
expect(console.warn.mock.calls[0][0]).toBe(
751735
'Warning: owner-based and parent-based contexts differ ' +
752736
'(values: `noise` vs `bar`) for key (foo) while mounting Component ' +
753737
'(see: http://fb.me/react-context-by-parent)'
754738
);
755-
expect(console.warn.mock.calls[1][0]).toBe(
756-
'Warning: owner-based and parent-based contexts differ ' +
757-
'(values: `noise` vs `bar`) for key (foo) while mounting ReactCompositeComponent ' +
758-
'(see: http://fb.me/react-context-by-parent)'
759-
);
760-
761739
});
762740

763741
it('unmasked context propagates through updates', function() {

0 commit comments

Comments
 (0)