-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if context value actually is undefined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't the context value tracked as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right. |
||
if ( | ||
contextType.$$typeof !== REACT_CONTEXT_TYPE && | ||
(contextType.$$typeof !== REACT_CONTEXT_TYPE || isContextConsumer) && | ||
!didWarnAboutInvalidateContextType.has(ctor) | ||
) { | ||
didWarnAboutInvalidateContextType.add(ctor); | ||
warningWithoutStack( | ||
false, | ||
'%s defines an invalid contextType. ' + | ||
'contextType should point to the Context object returned by React.createContext(). ' + | ||
'Did you accidentally pass the Context.Provider instead?', | ||
'Did you accidentally pass the Context.%s instead?', | ||
getComponentName(ctor) || 'Component', | ||
isContextConsumer ? 'Consumer' : 'Provider', | ||
); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 usedcontext
since we don't check for it in the context code paths.