Skip to content

Commit a9325e2

Browse files
committed
adds childContextTypes warnings for functional components
ReactStatelessComponent-test.js fails due to warnings not showing up when functional components mount or update. This PR ports the existing warnings from stack and re-applies them to fiber functional components and attempts to re-use some logic in ReactFiberContext that currently exists for showing warnings when dealing with class components.
1 parent 3d44a6a commit a9325e2

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

scripts/fiber/tests-passing-except-dev.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ src/renderers/__tests__/ReactHostOperationHistoryHook-test.js
139139
* gets reported when a child is inserted
140140
* gets reported when a child is removed
141141

142-
src/renderers/__tests__/ReactStatelessComponent-test.js
143-
* should warn for childContextTypes on a functional component
144-
145142
src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
146143
* should not warn when server-side rendering `onScroll`
147144
* should warn about incorrect casing on properties (ssr)

scripts/fiber/tests-passing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ src/renderers/__tests__/ReactStatelessComponent-test.js
777777
* should update stateless component
778778
* should unmount stateless component
779779
* should pass context thru stateless component
780+
* should warn for childContextTypes on a functional component
780781
* should throw when stateless component returns undefined
781782
* should throw on string refs in pure functions
782783
* should warn when given a string ref

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ var invariant = require('invariant');
6767
if (__DEV__) {
6868
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
6969
var warning = require('warning');
70+
var {
71+
warnAboutMissingGetChildContext,
72+
} = require('ReactFiberContext');
7073

7174
var warnedAboutStatelessRefs = {};
7275
}
@@ -479,6 +482,18 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
479482
// Proceed under the assumption that this is a functional component
480483
workInProgress.tag = FunctionalComponent;
481484
if (__DEV__) {
485+
const Component = workInProgress.type;
486+
487+
if (Component) {
488+
warning(
489+
!Component.childContextTypes,
490+
'%s(...): childContextTypes cannot be defined on a functional component.',
491+
Component.displayName || Component.name || 'Component'
492+
);
493+
if (Component.childContextTypes) {
494+
warnAboutMissingGetChildContext(workInProgress);
495+
}
496+
}
482497
if (workInProgress.ref !== null) {
483498
let info = '';
484499
const ownerName = ReactDebugCurrentFiber.getCurrentFiberOwnerName();

src/renderers/shared/fiber/ReactFiberContext.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,30 @@ const {
3232
push,
3333
} = require('ReactFiberStack');
3434

35+
var warnAboutMissingGetChildContext;
36+
3537
if (__DEV__) {
3638
var checkReactTypeSpec = require('checkReactTypeSpec');
3739
var warnedAboutMissingGetChildContext = {};
40+
41+
// TODO (bvaughn) Replace this behavior with an invariant() in the future.
42+
// It has only been added in Fiber to match the (unintentional) behavior in Stack.
43+
warnAboutMissingGetChildContext = (fiber: Fiber) => {
44+
const componentName = getComponentName(fiber);
45+
46+
if (!warnedAboutMissingGetChildContext[componentName]) {
47+
warnedAboutMissingGetChildContext[componentName] = true;
48+
warning(
49+
false,
50+
'%s.childContextTypes is specified but there is no getChildContext() method ' +
51+
'on the instance. You can either define getChildContext() on %s or remove ' +
52+
'childContextTypes from it.',
53+
componentName,
54+
componentName,
55+
);
56+
}
57+
};
58+
module.exports.warnAboutMissingGetChildContext = warnAboutMissingGetChildContext;
3859
}
3960

4061
// A cursor to the current merged context object on the stack.
@@ -144,23 +165,9 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
144165
const instance = fiber.stateNode;
145166
const childContextTypes = fiber.type.childContextTypes;
146167

147-
// TODO (bvaughn) Replace this behavior with an invariant() in the future.
148-
// It has only been added in Fiber to match the (unintentional) behavior in Stack.
149168
if (typeof instance.getChildContext !== 'function') {
150169
if (__DEV__) {
151-
const componentName = getComponentName(fiber);
152-
153-
if (!warnedAboutMissingGetChildContext[componentName]) {
154-
warnedAboutMissingGetChildContext[componentName] = true;
155-
warning(
156-
false,
157-
'%s.childContextTypes is specified but there is no getChildContext() method ' +
158-
'on the instance. You can either define getChildContext() on %s or remove ' +
159-
'childContextTypes from it.',
160-
componentName,
161-
componentName,
162-
);
163-
}
170+
warnAboutMissingGetChildContext(fiber);
164171
}
165172
return parentContext;
166173
}

0 commit comments

Comments
 (0)