Removed extra typeof checks for contextType.unstable_read (#13736)
Brian Vaughn committed
Sep 28, 2018 at 13:12 UTC
c5212646f8096fde506b40971f848f7641e4011b
2 files changed
+27
-38
packages/react-reconciler/src/ReactFiberClassComponent.js
+20
-35
@@ -352,21 +352,6 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
352
);
353
}
354
355
- if (
356
- ctor.contextType &&
357
- typeof ctor.contextType.unstable_read !== 'function' &&
358
- !didWarnAboutInvalidateContextType.has(ctor)
359
- ) {
360
- didWarnAboutInvalidateContextType.add(ctor);
361
- warningWithoutStack(
362
- false,
363
- '%s defines an invalid contextType. ' +
364
- 'contextType should point to the Context object returned by React.createContext(). ' +
365
- 'Did you accidentally pass the Context.Provider instead?',
366
- name,
367
- );
368
- }
369
-
355
const noComponentShouldUpdate =
356
typeof instance.componentShouldUpdate !== 'function';
357
warningWithoutStack(
@@ -520,11 +505,23 @@ function constructClassInstance(
505
let unmaskedContext = emptyContextObject;
506
let context = null;
507
const contextType = ctor.contextType;
523
- if (
524
- typeof contextType === 'object' &&
525
- contextType !== null &&
526
- typeof contextType.unstable_read === 'function'
527
- ) {
508
+ if (typeof contextType === 'object' && contextType !== null) {
509
+ if (__DEV__) {
510
+ if (
511
+ typeof contextType.unstable_read !== 'function' &&
512
+ !didWarnAboutInvalidateContextType.has(ctor)
513
+ ) {
514
+ didWarnAboutInvalidateContextType.add(ctor);
515
+ warningWithoutStack(
516
+ false,
517
+ '%s defines an invalid contextType. ' +
518
+ 'contextType should point to the Context object returned by React.createContext(). ' +
519
+ 'Did you accidentally pass the Context.Provider instead?',
520
+ getComponentName(ctor) || 'Component',
521
+ );
522
+ }
523
+ }
524
+
525
context = (contextType: any).unstable_read();
526
} else {
527
unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
@@ -727,11 +724,7 @@ function mountClassInstance(
724
instance.refs = emptyRefsObject;
725
726
const contextType = ctor.contextType;
730
- if (
731
- typeof contextType === 'object' &&
732
- contextType !== null &&
733
- typeof contextType.unstable_read === 'function'
734
- ) {
727
+ if (typeof contextType === 'object' && contextType !== null) {
728
instance.context = (contextType: any).unstable_read();
729
} else {
730
const unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
@@ -839,11 +832,7 @@ function resumeMountClassInstance(
832
const oldContext = instance.context;
833
const contextType = ctor.contextType;
834
let nextContext;
842
- if (
843
- typeof contextType === 'object' &&
844
- contextType !== null &&
845
- typeof contextType.unstable_read === 'function'
846
- ) {
835
+ if (typeof contextType === 'object' && contextType !== null) {
836
nextContext = (contextType: any).unstable_read();
837
} else {
838
const nextLegacyUnmaskedContext = getUnmaskedContext(
@@ -989,11 +978,7 @@ function updateClassInstance(
978
const oldContext = instance.context;
979
const contextType = ctor.contextType;
980
let nextContext;
992
- if (
993
- typeof contextType === 'object' &&
994
- contextType !== null &&
995
- typeof contextType.unstable_read === 'function'
996
- ) {
981
+ if (typeof contextType === 'object' && contextType !== null) {
982
nextContext = (contextType: any).unstable_read();
983
} else {
984
const nextUnmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
packages/react/src/__tests__/ReactContextValidator-test.js
+7
-3
@@ -555,7 +555,9 @@ describe('ReactContextValidator', () => {
555
}
556
}
557
558
- expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toWarnDev(
558
+ expect(() => {
559
+ expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toThrow();
560
+ }).toWarnDev(
561
'Warning: ComponentA defines an invalid contextType. ' +
562
'contextType should point to the Context object returned by React.createContext(). ' +
563
'Did you accidentally pass the Context.Provider instead?',
@@ -563,9 +565,11 @@ describe('ReactContextValidator', () => {
565
);
566
567
// Warnings should be deduped by component type
566
- ReactTestUtils.renderIntoDocument(<ComponentA />);
568
+ expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toThrow();
569
568
- expect(() => ReactTestUtils.renderIntoDocument(<ComponentB />)).toWarnDev(
570
+ expect(() => {
571
+ expect(() => ReactTestUtils.renderIntoDocument(<ComponentB />)).toThrow();
572
+ }).toWarnDev(
573
'Warning: ComponentB defines an invalid contextType. ' +
574
'contextType should point to the Context object returned by React.createContext(). ' +
575
'Did you accidentally pass the Context.Provider instead?',