Open Stophface opened 1 year ago
In my case the hook stops compare properly after the first successful comparison.
const ref = React.useRef<T>(value)
already has a value of dependencies (value: T) at a moment these two values are compared in if (!deepEqual(value, ref.current))
at a line 36.
I created a ref externally to that hook, and passed the ref through method calls. In this case the ref keeps a previous value.
Otherwise it seems that ref
and `value' share a same object reference.
For me it looks like a very strange behavior of useRef()
that might be caused with some decorator from some library.
UPD It does not matter whether the dependencies are memoized or not. The original useDeepCompare from react-use
has the same problem on my set of libraries.
In my case the hook stops compare properly after the first successful comparison. @dmytro-shchurov same for me
I am having a similar issue while depending on the value from the watch()
method from react-hook-form
. I am able to reproduce the behavior here:
https://codesandbox.io/s/modest-scooby-zpn2c7?file=/src/App.js
Modifying the input value does not seem to trigger the effect more than once. Any idea as to why?
E: I found this in the hook form docs:
watch result is optimised for render phase instead of useEffect's deps, to detect value update you may want to use an external custom hook for value comparison.
I'm guessing hook-form is the issue rather than this lib
E2: I was able to resolve this by using a slightly modified version of this library which does a cloneDeep
from this comment here
use-deep-compare-effect
version: ^1.8.1node
version: v18.11.0npm
version: 8.19.2Relevant code or config
What you did: I implement the
useDeepCompareEffect()
instead of the nativeuseEffect()
hook.Problem description: The
useDeepCompareEffect()
is not executing, when the object is updated. This oneis part of an offline first database (watermelonDB) and updates
arrayWithDeeplyNestedObjects
when there is a change in the database. I would expectuseDeepCompareEffect()
to execute, wheneverarrayWithDeeplyNestedObjects
changes, but it is not. This is howarrayWithDeeplyNestedObjects
looks likeThe changes to
arrayWithDeeplyNestedObjects
are done in the objects either tox
,y
orid_arr
. The length ofarrayWithDeeplyNestedObjects
can change as well. There might be more (or less) objects of the same structure in there.Why isn't
useDeepCompareEffect()
executed, whenarrayWithDeeplyNestedObjects
changes?