Fork me on GitHub
#helix
<
2020-11-24
>
tomc16:11:22

Would it make sense to add support for custom equality fns to use-effect, use-memo, etc? I'm in a situation now where I want to run an effect when a list of vectors changes, which as far as I can tell requires me to pull in/write something like https://github.com/kotarella1110/use-custom-compare.

dominicm17:11:29

@tomc I think react doesn't support that and helix is fairly light

lilactown17:11:22

we have a hook at work that we call use-stable-identity, which you can use like:

(hooks/use-effect
 [(use-stable-identity foo) bar baz]
 ,,,)
when you really need to do a deep comparison of values

lilactown17:11:41

this still doesn't cover the most annoying cases like, constructing a new Date or something 😓

lilactown17:11:35

I've thought about adding this to helix. I wouldn't add this to use-effect / use-memo though, but rather have it as a separate hook so you can opt in

lilactown17:11:55

@tomc are you reconstructing the vector with the same values over and over?

tomc17:11:25

@lilactown Yes I am, and I see now I don't have to. Thanks. For reference, a naive impl of custom equality for use-effect is pretty simple:

(defn use-custom-compare-memoize [deps eq?]
  (let [ref (h.hooks/use-ref [])]
    (if (or (not @ref) (not (eq? @ref deps)))
      (reset! ref deps))
    (into-array @ref)))

(defn use-custom-compare-effect [effect deps eq?]
  (react/useEffect effect (use-custom-compare-memoize deps eq?)))