question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useDeepCompareEffect should warn when dependencies includes function

See original GitHub issue
  • 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?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Apr 30, 2021

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

0reactions
github-actions[bot]commented, Sep 17, 2021

🎉 This issue has been resolved in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Untitled
The problem React's built-in `useEffect` hook has a second argument called the "dependencies array" and it allows you to optimize when React will...
Read more >
react useEffect comparing objects - Stack Overflow
2. This seems like a nice and simple solution when apiOptions is a not-too-complex data object. But I wonder: is it necessary to...
Read more >
A Lesser-Known Trick with React Hook Dependencies
signalRef is used to notify useMemo that the value has changed and return us the new one. The Trick. Do you know that...
Read more >
How to use the react-fast-compare function in react-fast-compare
console.warn( '`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.' ) } if (deps.every(isPrimitive)) ...
Read more >
Tips for Using React's UseEffect Effectively - Better Programming
The useEffect hook is basically a function('effect' as React calls) placed ... Not passing in a dependency array will run the callback on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found