Skip to content

Commit 330a90a

Browse files
bvaughnjetoneza
authored andcommitted
Removed extra typeof checks for contextType.unstable_read (facebook#13736)
1 parent 4a56f6e commit 330a90a

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,6 @@ function checkClassInstance(workInProgress: Fiber, ctor: any, newProps: any) {
352352
);
353353
}
354354

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-
370355
const noComponentShouldUpdate =
371356
typeof instance.componentShouldUpdate !== 'function';
372357
warningWithoutStack(
@@ -520,11 +505,23 @@ function constructClassInstance(
520505
let unmaskedContext = emptyContextObject;
521506
let context = null;
522507
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+
528525
context = (contextType: any).unstable_read();
529526
} else {
530527
unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
@@ -727,11 +724,7 @@ function mountClassInstance(
727724
instance.refs = emptyRefsObject;
728725

729726
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) {
735728
instance.context = (contextType: any).unstable_read();
736729
} else {
737730
const unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
@@ -839,11 +832,7 @@ function resumeMountClassInstance(
839832
const oldContext = instance.context;
840833
const contextType = ctor.contextType;
841834
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) {
847836
nextContext = (contextType: any).unstable_read();
848837
} else {
849838
const nextLegacyUnmaskedContext = getUnmaskedContext(
@@ -989,11 +978,7 @@ function updateClassInstance(
989978
const oldContext = instance.context;
990979
const contextType = ctor.contextType;
991980
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) {
997982
nextContext = (contextType: any).unstable_read();
998983
} else {
999984
const nextUnmaskedContext = getUnmaskedContext(workInProgress, ctor, true);

packages/react/src/__tests__/ReactContextValidator-test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,21 @@ describe('ReactContextValidator', () => {
555555
}
556556
}
557557

558-
expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toWarnDev(
558+
expect(() => {
559+
expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toThrow();
560+
}).toWarnDev(
559561
'Warning: ComponentA defines an invalid contextType. ' +
560562
'contextType should point to the Context object returned by React.createContext(). ' +
561563
'Did you accidentally pass the Context.Provider instead?',
562564
{withoutStack: true},
563565
);
564566

565567
// Warnings should be deduped by component type
566-
ReactTestUtils.renderIntoDocument(<ComponentA />);
568+
expect(() => ReactTestUtils.renderIntoDocument(<ComponentA />)).toThrow();
567569

568-
expect(() => ReactTestUtils.renderIntoDocument(<ComponentB />)).toWarnDev(
570+
expect(() => {
571+
expect(() => ReactTestUtils.renderIntoDocument(<ComponentB />)).toThrow();
572+
}).toWarnDev(
569573
'Warning: ComponentB defines an invalid contextType. ' +
570574
'contextType should point to the Context object returned by React.createContext(). ' +
571575
'Did you accidentally pass the Context.Provider instead?',

0 commit comments

Comments
 (0)