-
Notifications
You must be signed in to change notification settings - Fork 562
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
Why not make returning "undefined" bail out of component re-render? #131
Comments
...just an example where useMemo gets a little annoying. The Header part would only exist if there is data to show and would be defined in a useMemo depending only on "data" I would have to include conditionals for when the data is empty. In a case where data of the same type is added to the list it could be optimized the only depending on data[0]...but then you need to ensure data is always an array, and you lose some semantics for when data is It would be nice to just return a "Loading" or "NoData" components if data is undefined or empty respectively |
Returning |
Yes, I already posted about it a long time ago in the thread [Provide more ways to bail out inside Hooks · Issue #14110 · facebook/react]((No Title)) but the responses were not very helpful:
This would not be a breaking change. You "cannot" return I don't understand the willingness to not use the different semantics. Please try to provide more useful responses Thanks |
And by the way... react@experimental throwing when "not calling all hooks" is breaking thing for me right now and none of my proposals do |
This was enforced by https://reactjs.org/docs/hooks-rules.html#eslint-plugin all along, for good reason. |
Once again, I understand that "React relies on the order in which Hooks are called"!! |
well...just to keep this open facebook/react#17446 (comment) |
Hi, thanks for your suggestion. RFCs should be submitted as pull requests, not issues. I will close this issue but feel free to resubmit in the PR format. |
I may be missing something obvious but I still can't understand why the following is a bad idea
(maybe someone already explained it to me but... I'm not only slightly dumb but also very forgetful)
There's been discussion about how to implement some kind of "selector" in
useContext
and workaround methods using React.memo and I think canceling component re-render by returningundefined
could be a simple solution.Even in components that don't
useContext
could benefit by simplifying some memoization hedge cases.I've been using a HOC that simply returns the last wrapped component render value if it returns
undefined
but unfortunately "react@experimental" now forces that "all" hooks be called and that completely invalidates this all bailout system.Why not allow for components that return before all hooks are called as long has they're called in the right order? (and of course...implement the "cancel on undefined" natively)
Then using a simple hook like this:
one could:
Calling the component function (that would return early) should be more worst the calling some selector function.
Also, is that changeBits thingy really such a performance boost over some shallow comparison?
One thing I remember being told is that returning
undefined
is kind of "ugly" but... couldn't really understand why.With this you don't need a context selector nor even a React.memo HOC
Would someone tell me what's the catch with this approach?
Thanks
The text was updated successfully, but these errors were encountered: