Improve warning for invalid class contextType#15142
Conversation
|
ReactDOM: size: 0.0%, gzip: -0.0% Details of bundled changes.Comparing: df7b87d...a123591 react-dom
react-art
react-native-renderer
react-test-renderer
react-reconciler
Generated by 🚫 dangerJS |
| 'This can also happen due to a circular dependency, so ' + | ||
| 'try moving the createContext() call to a separate file.'; | ||
| } else if (typeof contextType !== 'object') { | ||
| addendum = ' However, it is set to ' + typeof contextType + '.'; |
There was a problem hiding this comment.
I think we can add "a" here to make it a little clearer
addendum = ' However, it is set to a ' + typeof contextType + '.';That way it reports "set to a string" instead of "set to string", which sounds better. Same goes for numbers, symbols, and functions.
| } else if (contextType._context !== undefined) { | ||
| // <Context.Consumer> | ||
| addendum = ' Did you accidentally pass the Context.Consumer instead?'; | ||
| } else { |
There was a problem hiding this comment.
Add another branch for arrays?
else if (Array.isArray(contextType)) {
addendum = 'However, it is set to an array.'
}There was a problem hiding this comment.
Seems very unlikely to happen
There was a problem hiding this comment.
🤷♂️ seems like it could happen if you import the wrong value. I figured that if it happens it would be weird to see.
However, it is set to an object with keys {0, 1, 2, 3, 4, 5, 6...100000}
There was a problem hiding this comment.
Arrays are pretty rare as export values so I don't think it's worth handling.
| didWarnAboutInvalidateContextType.add(type); | ||
|
|
||
| let addendum = ''; | ||
| if (contextType === null) { |
There was a problem hiding this comment.
Sometimes it is nice to be able to conditionally set it:
class Foo {
static contextType = isServerEnvironment ? null : Context;
}This provides no way to do that while also ensuring you stay declarative.
Is null really a common mistake? I'm guessing not since it's the only one you don't have a descriptive message for.
There was a problem hiding this comment.
Nope. Agree let’s omit it.
* Improve warning for invalid class contextType * Don't warn for null * Grammar
This adds extra warning for when
contextTypeexists but isundefined. This can happen due to module cycles in legacy environments (see #13969) and can be very difficult to debug.SSR and regular version are copy pasted.
I separated the DEV check outside the actual code because unlike in the prod code, we actually do want to warn for
undefined-but-not-truthy code path.