Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about missing getChildContext method #8742

Merged
merged 4 commits into from
Jan 13, 2017

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Jan 10, 2017

Previous (probably unintentional) behavior of Stack was to allow components to define childContextTypes without also supplying a getChildContext property. This PR updates Fiber to temporarily mimic that behavior. It also adds warning messages to both Fiber and Stack along with a some tests.

For the time being Fiber components with a missing getChildContext method will return the parent context as-is, after warning.

// It has only been added in Fiber to match the (unintentional) behavior in Stack.
if (typeof instance.getChildContext !== 'function') {
warning(
false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should deduplicate this by component name. Otherwise it can get very noisy.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example of how we do it #8739.

This PR would be much simpler though. Since warning is specific to a class, we can use component name as the key in the warnedAboutMissingChildContext cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion Dan. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deduped and a new test added

@bvaughn bvaughn force-pushed the stack-fiber-gcc-warning branch 2 times, most recently from edf41b5 to e972b89 Compare January 10, 2017 22:01
@gaearon gaearon dismissed their stale review January 10, 2017 22:08

Now fixed.

@bvaughn bvaughn changed the title Stack and Fiber warn about missing getChildContext method Warn about missing getChildContext method Jan 10, 2017
Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepting with nitpicks.

@@ -33,6 +34,7 @@ const {

if (__DEV__) {
var checkReactTypeSpec = require('checkReactTypeSpec');
var warningAboutMissingGetChildContext = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we name this warnedAboutMissingGetChildContext?

warningAboutMissingGetChildContext[componentName] = true;
warning(
false,
'getChildContext() is not defined for %s',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let’s make it a bit more descriptive? We can make it similar in style to messages in ReactFiberClassComponent (and Stack equivalents in ReactCompositeComponent). For example:

'%s.childContextTypes is specified but there is no getChildContext() method on the instance. You can either define getChildContext() on %s or remove childContextTypes from it.',

it('should pass parent context if getChildContext method is missing', () => {
spyOn(console, 'error');

var ParentContextProvider = React.createClass({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any special reason for using createClass here? Ideally we'd like to use ES6 for testing anything other than createClass itself specifically.

Copy link
Contributor Author

@bvaughn bvaughn Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just mirroring what's in the rest of the class. I'll replace it with ES6 syntax though. I prefer that anyway.

Wrote this test first, mirrored the rest of the class, wrote the other test afterward and used ES6. Not sure why. 😅

Brian Vaughn added 3 commits January 12, 2017 16:58
Previous (probably unintentional) behavior of Stack was to allow components to define childContextTypes without also supplying a getChildContext property. This PR updates Fiber to (temporarily) mimic that behavior. It also adds warning messages to both Fiber and Stack (along with a test).

For the time being, Fiber components with a missing getChildContext method will return the parent context as-is, after warning.
@bvaughn bvaughn force-pushed the stack-fiber-gcc-warning branch from e3d06ed to 7a2e35b Compare January 13, 2017 01:12
@bvaughn bvaughn merged commit 3bc5595 into facebook:master Jan 13, 2017
@bvaughn bvaughn deleted the stack-fiber-gcc-warning branch January 13, 2017 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants