Skip to content

Commit 23f2451

Browse files
authored
Merge pull request #9043 from trueadm/fix-fiber-functional-componment-childcontext
[Fiber] adds `childContextTypes` warnings for functional components
2 parents 5f6f327 + bde5df1 commit 23f2451

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

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

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

135-
src/renderers/__tests__/ReactStatelessComponent-test.js
136-
* should warn for childContextTypes on a functional component
137-
138135
src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
139136
* should not warn when server-side rendering `onScroll`
140137
* 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
@@ -784,6 +784,7 @@ src/renderers/__tests__/ReactStatelessComponent-test.js
784784
* should update stateless component
785785
* should unmount stateless component
786786
* should pass context thru stateless component
787+
* should warn for childContextTypes on a functional component
787788
* should throw when stateless component returns undefined
788789
* should throw on string refs in pure functions
789790
* should warn when given a string ref

src/renderers/__tests__/ReactStatelessComponent-test.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,26 @@ describe('ReactStatelessComponent', () => {
118118

119119
ReactDOM.render(<StatelessComponentWithChildContext name="A" />, container);
120120

121-
expectDev(console.error.calls.count()).toBe(2);
122-
expectDev(console.error.calls.argsFor(0)[0]).toContain(
123-
'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
124-
'be defined on a functional component.'
125-
);
126-
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
127-
'Warning: StatelessComponentWithChildContext.childContextTypes is specified ' +
128-
'but there is no getChildContext() method on the instance. You can either ' +
129-
'define getChildContext() on StatelessComponentWithChildContext or remove ' +
130-
'childContextTypes from it.'
131-
);
121+
// Stack and Fiber differ in terms of they show warnings
122+
if (ReactDOMFeatureFlags.useFiber) {
123+
expectDev(console.error.calls.count()).toBe(1);
124+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
125+
'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
126+
'be defined on a functional component.'
127+
);
128+
} else {
129+
expectDev(console.error.calls.count()).toBe(2);
130+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
131+
'StatelessComponentWithChildContext(...): childContextTypes cannot ' +
132+
'be defined on a functional component.'
133+
);
134+
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
135+
'Warning: StatelessComponentWithChildContext.childContextTypes is specified ' +
136+
'but there is no getChildContext() method on the instance. You can either ' +
137+
'define getChildContext() on StatelessComponentWithChildContext or remove ' +
138+
'childContextTypes from it.'
139+
);
140+
}
132141
});
133142

134143
if (!ReactDOMFeatureFlags.useFiber) {

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ var invariant = require('invariant');
6767
if (__DEV__) {
6868
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
6969
var warning = require('warning');
70-
7170
var warnedAboutStatelessRefs = {};
7271
}
7372

@@ -479,6 +478,15 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
479478
// Proceed under the assumption that this is a functional component
480479
workInProgress.tag = FunctionalComponent;
481480
if (__DEV__) {
481+
const Component = workInProgress.type;
482+
483+
if (Component) {
484+
warning(
485+
!Component.childContextTypes,
486+
'%s(...): childContextTypes cannot be defined on a functional component.',
487+
Component.displayName || Component.name || 'Component'
488+
);
489+
}
482490
if (workInProgress.ref !== null) {
483491
let info = '';
484492
const ownerName = ReactDebugCurrentFiber.getCurrentFiberOwnerName();

0 commit comments

Comments
 (0)