-
Notifications
You must be signed in to change notification settings - Fork 47k
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
passing React.createContext() mistakenly as if it were an element causes confusing error #13624
Comments
Maybe the first warning could be amended to be more suggestive about the causes. |
Also worth noting is that documentation suggest good practice using destructuring assignment when initializing Context which prevents from mentioned mistake: P.S. @gaearon Maybe all examples on the Context page and in the 16.3 release blog post should be updated according to good practice suggested in |
I think I will take this up. I will update the warning for such instance to be more suggestive about the cause |
@gaearon is there any other thing I need to know to be able to resolve this issue. Thanks. |
I think this has been updated. |
#13829 |
I agree with this. The usage below is valid: const {Provider: ColorProvider, Consumer: ColorConsumer} = React.createContext();
function Welcome() {
return (
<ColorConsumer>
{
color =>
<div><h1>Hello, {color} </h1>
<h3> {color}</h3>
</div>
}
</ColorConsumer>
);
}
function App() {
return (
<ColorProvider value="Green" >
<div>
<Welcome />
</div>
</ColorProvider>
);
}
ReactDOM.render(<App />, document.getElementById('root')); |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
If you're really tired and forget to use a
Context.Provider
, and instead use theContext
as if it were a provider, React (understandably) freaks out, but the error message is confusing. i.e.:This results in the console looking something like (pasted from codesandbox):
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
What is the expected behavior?
An error that (roughly paraphrased) says something along the lines of:
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.3+
The text was updated successfully, but these errors were encountered: