-
Notifications
You must be signed in to change notification settings - Fork 799
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 unnecessarily update #1365
Comments
Yes, unfortunately, this is how it works. A first point here - #1339, hot sure it would be ever fixed in RHL |
@theKashey Thanks. I'm glad to know that this is normal and not just me doing something wrong. Something I noticed in the code by the way is this: react-hot-loader/src/reactHotLoader.js Lines 35 to 47 in c89e306
There's something that checks if the But then I read ahead, and there's that bit where it says if the hook has at least 1 dependency, then doesn't matter if the body didn't change, and doesn't even matter if the dependencies didn't changed: reloading of that hook happens, period. Is there any reason why that is, and why the body and dependencies couldn't be checked there? EDIT: by commenting out EDIT 2: Like what @theKashey said below, the downside to this approach is that if a hook depends on functions/values from outside the hook, and even if the hooks only reloaded on their bodies or dependencies changing, they wouldn't be able to detect the outside values/functions (that are not dependencies) to automatically reload; thus a manual reload would be needed in those cases. |
Your hooks might use some other functions inside them, which might be changed, and that is not trackable using
|
@theKashey yes, indeed that is what's happening. Thanks. I'll update my comment to reflect the downside of my approach. Hopefully this issue will help other people avoid wasting a lot of time trying to get hook hot reloading to work "better". |
@theKashey actually, what about if more dependencies would be added to the hook? So this is how you'd normally write a useEffect(() => {
dispatch(loadPosts());
}, [dispatch]); But this could possibly be done too:
|
Well, React-Fast-Refresh, an upcoming replacement for React-Hot-Loader, would update hooks even without any dependencies. However, not all, but only in locations, "affected" by a change (read even more unpredictable). This is why "not sure it would be ever fixed in RHL" - there is no much sense to spend time fixing it. |
Whenever I make changes to a component that doesn't even involve hooks, all hooks with dependencies in them get updated undesirably. E.g.:
So any tiny text change I make anywhere, this
useEffect()
callback gets called, causing my spinner to show, and load my posts again unnecessarily.If I removed
dispatch
from the hook dependencies, then theuseEffect()
does not get called, just what I want. But I violate the react-hooks/exhaustive-deps eslint rule that way.Also, if I add any static value to the dependencies, then also, it always gets updated, which is not what I want. Note that each time I don't edit the hook itself, and in fact not even the same file as the hook.
Is this normal?
The text was updated successfully, but these errors were encountered: