Improved error message wording for missing getChildContext() method
Brian Vaughn committed
Jan 12, 2017 at 17:11 UTC
7a2e35b93b20f702c274dcfcddf4a184e82a7cc3
4 files changed
+35
-23
src/isomorphic/classic/__tests__/ReactContextValidator-test.js
+20
-16
@@ -314,7 +314,9 @@ describe('ReactContextValidator', () => {
314
ReactTestUtils.renderIntoDocument(<ComponentA/>);
315
expectDev(console.error.calls.count()).toBe(1);
316
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
317
- 'Warning: getChildContext() is not defined for ComponentA'
317
+ 'Warning: ComponentA.childContextTypes is specified but there is no ' +
318
+ 'getChildContext() method on the instance. You can either define ' +
319
+ 'getChildContext() on ComponentA or remove childContextTypes from it.'
320
);
321
322
// Warnings should be deduped by component type
@@ -323,7 +325,9 @@ describe('ReactContextValidator', () => {
325
ReactTestUtils.renderIntoDocument(<ComponentB/>);
326
expectDev(console.error.calls.count()).toBe(2);
327
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
326
- 'Warning: getChildContext() is not defined for ComponentB'
328
+ 'Warning: ComponentB.childContextTypes is specified but there is no ' +
329
+ 'getChildContext() method on the instance. You can either define ' +
330
+ 'getChildContext() on ComponentB or remove childContextTypes from it.'
331
);
332
});
333
@@ -332,28 +336,28 @@ describe('ReactContextValidator', () => {
336
it('should pass parent context if getChildContext method is missing', () => {
337
spyOn(console, 'error');
338
335
- var ParentContextProvider = React.createClass({
336
- childContextTypes: {
339
+ class ParentContextProvider extends React.Component {
340
+ static childContextTypes = {
341
foo: React.PropTypes.number,
338
- },
339
- getChildContext: function() {
342
+ };
343
+ getChildContext() {
344
return {
345
foo: 'FOO',
346
};
343
- },
344
- render: function() {
347
+ }
348
+ render() {
349
return <MiddleMissingContext />;
346
- },
347
- });
350
+ }
351
+ }
352
349
- var MiddleMissingContext = React.createClass({
350
- childContextTypes: {
353
+ class MiddleMissingContext extends React.Component {
354
+ static childContextTypes = {
355
bar: React.PropTypes.string.isRequired,
352
- },
353
- render: function() {
356
+ };
357
+ render() {
358
return <ChildContextConsumer />;
355
- },
356
- });
359
+ }
360
+ }
361
362
var childContext;
363
var ChildContextConsumer = React.createClass({
src/renderers/shared/fiber/ReactFiberContext.js
+7
-4
@@ -34,7 +34,7 @@ const {
34
35
if (__DEV__) {
36
var checkReactTypeSpec = require('checkReactTypeSpec');
37
- var warningAboutMissingGetChildContext = {};
37
+ var warnedAboutMissingGetChildContext = {};
38
}
39
40
// A cursor to the current merged context object on the stack.
@@ -150,11 +150,14 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
150
if (__DEV__) {
151
const componentName = getComponentName(fiber);
152
153
- if (!warningAboutMissingGetChildContext[componentName]) {
154
- warningAboutMissingGetChildContext[componentName] = true;
153
+ if (!warnedAboutMissingGetChildContext[componentName]) {
154
+ warnedAboutMissingGetChildContext[componentName] = true;
155
warning(
156
false,
157
- 'getChildContext() is not defined for %s',
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
}
src/renderers/shared/shared/__tests__/ReactStatelessComponent-test.js
+4
-2
@@ -124,8 +124,10 @@ describe('ReactStatelessComponent', () => {
124
'be defined on a functional component.'
125
);
126
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
127
- 'Warning: getChildContext() is not defined for ' +
128
- 'StatelessComponentWithChildContext'
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
);
132
});
133
src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
+4
-1
@@ -707,7 +707,10 @@ var ReactCompositeComponent = {
707
warningAboutMissingGetChildContext[componentName] = true;
708
warning(
709
!Component.childContextTypes,
710
- 'getChildContext() is not defined for %s',
710
+ '%s.childContextTypes is specified but there is no getChildContext() method ' +
711
+ 'on the instance. You can either define getChildContext() on %s or remove ' +
712
+ 'childContextTypes from it.',
713
+ componentName,
714
componentName,
715
);
716
}