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 for Context.Consumer with contextType #14831

Merged
merged 1 commit into from
Mar 18, 2019

Conversation

aweary
Copy link
Contributor

@aweary aweary commented Feb 12, 2019

Fixes #14793

Extends the contextType warning to include Context.Consumer. For both the ReactDOM and ReactDomServer.

Since $$typeof alone can't be used to distinguish consumers from context objects, the heuristic I used to identify context consumers was to check for the _context property which doesn't exist on the context object itself.

if (contextType.$$typeof !== REACT_CONTEXT_TYPE) {
const isContextConsumer =
contextType.$$typeof === REACT_CONTEXT_TYPE &&
contextType._context !== undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was it also called context in some patch release?

Copy link
Contributor Author

@aweary aweary Feb 12, 2019

Choose a reason for hiding this comment

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

Yeah, it was changed to _context in #12501. Is it worth checking? I think a lot more would be broken if you were trying to render a tree created with a version of React that still used context since we don't check for it in the context code paths.

@aweary aweary requested a review from gaearon February 25, 2019 17:59
@@ -515,17 +515,21 @@ function constructClassInstance(
const contextType = ctor.contextType;
if (typeof contextType === 'object' && contextType !== null) {
if (__DEV__) {
const isContextConsumer =
contextType.$$typeof === REACT_CONTEXT_TYPE &&
contextType._context !== undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if context value actually is undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't the context value tracked as _currentValue on this _context element?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh right.

@gaearon gaearon merged commit df7b87d into facebook:master Mar 18, 2019
gaearon pushed a commit to gaearon/react that referenced this pull request Mar 27, 2019
@gaearon gaearon mentioned this pull request Mar 27, 2019
This was referenced Aug 12, 2019
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