Skip to content

Commit 7a2e35b

Browse files
author
Brian Vaughn
committed
Improved error message wording for missing getChildContext() method
1 parent e17cc98 commit 7a2e35b

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

src/isomorphic/classic/__tests__/ReactContextValidator-test.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ describe('ReactContextValidator', () => {
314314
ReactTestUtils.renderIntoDocument(<ComponentA/>);
315315
expectDev(console.error.calls.count()).toBe(1);
316316
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.'
318320
);
319321

320322
// Warnings should be deduped by component type
@@ -323,7 +325,9 @@ describe('ReactContextValidator', () => {
323325
ReactTestUtils.renderIntoDocument(<ComponentB/>);
324326
expectDev(console.error.calls.count()).toBe(2);
325327
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.'
327331
);
328332
});
329333

@@ -332,28 +336,28 @@ describe('ReactContextValidator', () => {
332336
it('should pass parent context if getChildContext method is missing', () => {
333337
spyOn(console, 'error');
334338

335-
var ParentContextProvider = React.createClass({
336-
childContextTypes: {
339+
class ParentContextProvider extends React.Component {
340+
static childContextTypes = {
337341
foo: React.PropTypes.number,
338-
},
339-
getChildContext: function() {
342+
};
343+
getChildContext() {
340344
return {
341345
foo: 'FOO',
342346
};
343-
},
344-
render: function() {
347+
}
348+
render() {
345349
return <MiddleMissingContext />;
346-
},
347-
});
350+
}
351+
}
348352

349-
var MiddleMissingContext = React.createClass({
350-
childContextTypes: {
353+
class MiddleMissingContext extends React.Component {
354+
static childContextTypes = {
351355
bar: React.PropTypes.string.isRequired,
352-
},
353-
render: function() {
356+
};
357+
render() {
354358
return <ChildContextConsumer />;
355-
},
356-
});
359+
}
360+
}
357361

358362
var childContext;
359363
var ChildContextConsumer = React.createClass({

src/renderers/shared/fiber/ReactFiberContext.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const {
3434

3535
if (__DEV__) {
3636
var checkReactTypeSpec = require('checkReactTypeSpec');
37-
var warningAboutMissingGetChildContext = {};
37+
var warnedAboutMissingGetChildContext = {};
3838
}
3939

4040
// A cursor to the current merged context object on the stack.
@@ -150,11 +150,14 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
150150
if (__DEV__) {
151151
const componentName = getComponentName(fiber);
152152

153-
if (!warningAboutMissingGetChildContext[componentName]) {
154-
warningAboutMissingGetChildContext[componentName] = true;
153+
if (!warnedAboutMissingGetChildContext[componentName]) {
154+
warnedAboutMissingGetChildContext[componentName] = true;
155155
warning(
156156
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,
158161
componentName,
159162
);
160163
}

src/renderers/shared/shared/__tests__/ReactStatelessComponent-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ describe('ReactStatelessComponent', () => {
124124
'be defined on a functional component.'
125125
);
126126
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.'
129131
);
130132
});
131133

src/renderers/shared/stack/reconciler/ReactCompositeComponent.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,10 @@ var ReactCompositeComponent = {
707707
warningAboutMissingGetChildContext[componentName] = true;
708708
warning(
709709
!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,
711714
componentName,
712715
);
713716
}

0 commit comments

Comments
 (0)