I actually encountered similar issue but arrived at a different solution:
function useMemo<T>(reference: Ref<T>, getKey: (v: T) => unknown = v => v) {
let memo: Ref<T>;
return computed(() => {
if (!memo) {
memo = ref(reference.value) as Ref<T>;
watch(
() => getKey(reference.value),
(key, prevKey) => {
memo.value = reference.value;
},
{ flush: 'sync' }
);
}
return memo.value;
});
}
Wrote a useMemo function that returns a wrapped reference that triggers downstream watcher only when some memorization key from the original reference changed. Hence combining the best of computed and watch.
Need to point out that there some difference in my requirement hence eagerComputed not suitable. In my case
I need the computed to stay lazy.
The computed value is an object reconstructed on every compute so referential equality used in eagerComputed will fail.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I actually encountered similar issue but arrived at a different solution:
Wrote a
useMemofunction that returns a wrapped reference that triggers downstream watcher only when some memorizationkeyfrom the original reference changed. Hence combining the best ofcomputedandwatch.Need to point out that there some difference in my requirement hence
eagerComputednot suitable. In my caseeagerComputedwill fail.