This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-03
Channels
- # announcements (12)
- # babashka (36)
- # beginners (126)
- # calva (26)
- # cider (10)
- # clj-kondo (71)
- # cljdoc (3)
- # cljsrn (2)
- # clojure (232)
- # clojure-australia (1)
- # clojure-europe (11)
- # clojure-france (20)
- # clojure-nl (4)
- # clojure-norway (1)
- # clojure-serbia (4)
- # clojure-uk (6)
- # clojurescript (62)
- # conjure (5)
- # cursive (12)
- # data-science (1)
- # datomic (57)
- # deps-new (1)
- # duct (3)
- # emacs (5)
- # events (8)
- # fulcro (6)
- # graalvm (5)
- # helix (3)
- # jobs (6)
- # jobs-discuss (3)
- # kaocha (4)
- # lsp (128)
- # malli (12)
- # missionary (22)
- # off-topic (1)
- # pathom (7)
- # polylith (27)
- # quil (1)
- # re-frame (20)
- # react (9)
- # reitit (12)
- # releases (8)
- # remote-jobs (3)
- # sci (3)
- # shadow-cljs (9)
- # spacemacs (10)
- # tools-deps (7)
- # vim (7)
- # xtdb (14)
Anyone happen to have an implementation of this useMemoCompare
they could share? https://usehooks.com/useMemoCompare/
Looks like a generally interesting approach to deal with complex data structures in useEffect
dependencies
I think I can also just use useMemo
for my particular use case
I do have a hook that I call use-stable-identity
which uses Clojure equality to always return the previous one if the value passed in to is =
(defn use-stable-identity
"Caches `x`. When a new `x` is passed in, returns new `x` only if it is
not structurally equal to the previous `x`.
Useful for optimizing `use-effect`, `use-memo` et. al. when you have two
values that might be structurally equal but referentially different."
[x]
(let [-x (react/useRef x)
;; if they are equal, return the prev one to ensure ref equality
x' (if (= x (.-current -x))
(.-current -x)
x)]
;; Set the ref to be the last value that was succesfully used to render
(react/useEffect (fn []
(set! (.-current -x) x)
js/undefined)
#js [x'])
x'))
it's basically useMemoCompare
but assumes =
is the compare you want to use. you could parameterize that
IME it's only really useful in a couple circumstances, for instance you've got a complex data structure that is being serialized from somewhere (i.e. a network request, or local storage), changes relatively infrequently and you want to detect if it's actually different than the previous value