This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-14
Channels
- # adventofcode (38)
- # announcements (42)
- # aws (3)
- # babashka (8)
- # beginners (165)
- # calva (36)
- # chlorine-clover (14)
- # cider (6)
- # clj-kondo (5)
- # cljsrn (33)
- # clojure (27)
- # clojure-australia (1)
- # clojure-czech (1)
- # clojure-doc (1)
- # clojure-europe (26)
- # clojure-nl (6)
- # clojure-spec (6)
- # clojure-uk (3)
- # clojurescript (10)
- # code-reviews (20)
- # conjure (1)
- # core-logic (5)
- # cursive (3)
- # data-science (5)
- # datomic (35)
- # emacs (1)
- # figwheel-main (3)
- # fulcro (10)
- # honeysql (1)
- # introduce-yourself (4)
- # jobs (3)
- # jobs-discuss (4)
- # minecraft (2)
- # missionary (28)
- # nextjournal (3)
- # off-topic (45)
- # pathom (7)
- # polylith (1)
- # portal (22)
- # practicalli (2)
- # re-frame (4)
- # reagent (19)
- # releases (3)
- # remote-jobs (3)
- # reveal (1)
- # rum (4)
- # shadow-cljs (37)
- # spacemacs (14)
- # sql (1)
- # tools-build (7)
- # tools-deps (16)
- # vim (13)
- # xtdb (15)
What happens if you dereference ratoms in the outer function of a form-2 component? Will it simply never update?
Here is my scenario: I want to display some async result. So I have a component that sets up a callback based on its parameters, and then I guess keeps some local ratom that the callback can update. Problem is, that would rerender the component, which would also set up a new callback.
What I have done now is have a ratom, which has a watch that does the callback which sets another atom
Yea it's fine. My initial plan was to have the second atom as local state but that gave complications
I think in React you would use the useEffect
hook with two arguments. Can't really think of a Reagent alternative off the top of my head.
I'll just stick to the global state solution, it's not that bad
I'm not sure why this code doesn't rerender:
(defn DocsBad []
(let [state (r/atom {:versions nil})]
(fn [article-id]
(let [state* @state
{:keys [versions]} @state
version-entity-ids [1 2]]
(when (not= versions version-entity-ids)
(swap! state assoc :versions (vec version-entity-ids)))
[:div (pr-str state* @state)]))))
The pr-str gives {:versions nil} {:versions [1 2]}
if nothing external causes a rerender
I checked everything on https://cljdoc.org/d/reagent/reagent/1.1.0/doc/frequently-asked-questions/why-isn-t-my-component-re-rendering-
If I change the swap! to (js/setTimeout #(swap! state assoc :versions (vec version-entity-ids)))
, it rerenders perfectly (`{:versions [1 2]} {:versions [1 2]}`)you set state
to {:versions nil}
, and then deref it to state*
, so state*
is now {:versions nil}
, not <reagent.core/atom {:versions nil}>
, and then you swap!
the value in state
, but that doesn’t change state*
because why would it?
state*
is just a persistent hashmap
Because I expect it to get rerendered after every change to state
so the first render should show nil, but it should rerender immediately with the new value
i wonder if it doesn’t trigger a rerender because that happens before the hiccup is returned
Maybe
It's easy to work around, but it took me a long time to isolate it, so it should probably be an entry on that page