Skip to content
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

useDeepCompareEffect should warn when dependencies includes function #47

Closed
jasperck opened this issue Apr 29, 2021 · 5 comments
Closed
Labels

Comments

@jasperck
Copy link
Contributor

jasperck commented Apr 29, 2021

  • use-deep-compare-effect version: 1.6.1

What you did:

Pass a nested object with/without a function as deps

What happened:

A nested object with property has function as value will break deep compare and trigger the hooks

Reproduction repository:

https://codesandbox.io/s/use-deep-compare-effect-repro-fn-break-compare-mt5tk

Problem description:

Given how dequal do the comparison plus how people pass function as props in React, effect callback might has chance to be called while we thought no properties were changed. Ideally, we will destruct props and pass as deps, or wrap function with useCallback when passing it for deps comparison, but with use-deep-compare-effect handling nested object, we might not realize the risk and just pass the whole object includes function to the deps array, in this case, the comparison will failed which calling the effect callback every time we click the button.

Suggested solution:

Not sure if we can handle this more properly here, but I'm thinking at least a warning would be very helpful for people who try to debug the source of triggering the hooks, e.g. when identify function during checkDeps raise a warning like useDeepCompareEffect would be triggered when dependencies has function, make sure this is expected behavior, otherwise, handle it properly. What do you think?

@jasperck jasperck changed the title useDeepCompareEffect should warn when dependencies includes function as property value useDeepCompareEffect should warn when dependencies includes function Apr 29, 2021
@kentcdodds
Copy link
Owner

Hi @jasperck,

Unfortunately providing a function isn't necessarily wrong and there's no way for us to know if you're doing it wrong or right. For example: https://codesandbox.io/s/use-deep-compare-effect-repro-fn-break-compare-forked-sqidp?file=/src/App.js

So I don't think there's anything we can do about this.

@jasperck
Copy link
Contributor Author

Hey @kentcdodds,

providing a function isn't necessarily wrong and there's no way for us to know if you're doing it wrong or right

I totally agree with this, that's why I am purposing to add a warning but not error, because I think it's annoying that the hooks will be triggered out of the expectation while we thought deep compare handle things under the hood.

But thinking of when we have several unstable function in such case, logging warning probably is not an ideal developer experience, another thought is update README to make sure people know this scenario, what do you think?

@kentcdodds
Copy link
Owner

An update to the README would be welcome 👍 good idea.

@jasperck jasperck mentioned this issue May 11, 2021
3 tasks
@jasperck
Copy link
Contributor Author

Hey @kentcdodds, sorry I was AWK last week. Thanks for the feedback and I just opened a PR 👆🙏

kentcdodds added a commit that referenced this issue May 11, 2021
Closes #47 

Co-authored-by: Kent C. Dodds <[email protected]>
@github-actions
Copy link

🎉 This issue has been resolved in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants