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.
@tomc I think react doesn't support that and helix is fairly light
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 valuesthis still doesn't cover the most annoying cases like, constructing a new Date
or something 😓
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
@tomc are you reconstructing the vector with the same values over and over?
@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?)))