-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
Hooks: Storing a React component as state produces an unintuitive error #14186
Comments
That is a known possible point of confusion with You probably want to use |
Hrm, that seems like an ergonomics problem, given that I've run into a similar scenario before with the API design in one of my own projects, and there I eventually decided to just have separate I'm not convinced that it's possible to merge eager and lazy usage into a single API without causing such issues. Perhaps React should make a similar distinction. |
This is where using a type system would help you 😉 |
You’re welcome to comment on the RFC if you have an alternative proposal. We’ll read all comments there. Indeed this is an intentional decision even though we recognise the downsides. |
Do you want to request a feature or report a bug?
Bug.
What is the current behavior?
Storing a function component as state using
useState
is interpreted as providing a lazy state function, and React then attempts to use the returned rendered result as a component.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:
(Note: The context-related code is not relevant to this bug. I'm just reusing the same skeleton code for tracking down another bug that is context-related. But that one is probably a bug in my own code.)
What is the expected behavior?
React components - functional or otherwise - should be treated as state values rather than as lazy functions.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.7.0-alpha0. Earlier versions not applicable, since this is about hooks.
The text was updated successfully, but these errors were encountered: