This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-30
Channels
- # adventofcode (7)
- # announcements (9)
- # babashka (20)
- # beginners (182)
- # calva (9)
- # cider (20)
- # circleci (3)
- # clj-kondo (1)
- # clojure (269)
- # clojure-europe (2)
- # clojure-india (1)
- # clojure-italy (6)
- # clojure-nl (5)
- # clojure-uk (50)
- # clojurescript (56)
- # code-reviews (3)
- # core-async (174)
- # datomic (4)
- # duct (1)
- # emacs (3)
- # events (1)
- # fulcro (31)
- # graalvm (10)
- # graphql (8)
- # jobs (1)
- # joker (11)
- # juxt (7)
- # luminus (2)
- # malli (4)
- # off-topic (2)
- # overtone (1)
- # pathom (2)
- # re-frame (24)
- # shadow-cljs (42)
- # sql (1)
- # tools-deps (10)
What am I doing wrong...I can't get my UI element to react to my DB changes?
(ns dhl-oms.sample
(:require
[reagent.core :as r]
[re-frame.core :as rf]))
(defn create-user [cofx evt]
(let [username (evt 1)
db (:db cofx)]
{:db (assoc db :user username)}))
(rf/reg-event-fx :create-user create-user)
(rf/reg-sub :user #(-> % :user))
(defn main-panel []
(let [user (rf/subscribe [:user])]
[:div
[:p "User value: >" (some-> @user :user) "<"]
[:button
{:on-click
#(rf/dispatch [:create-user "Bob"])}
"Create user!"]]))
(defn mount []
(r/render
[main-panel]
(js/document.getElementById "app")))
(mount)
(comment
;; load page
@(rf/subscribe [:user]) ;; => nil
;; click button
@(rf/subscribe [:user]) ;; => "Bob"
;; but no page refresh?
)
(evt 1)
= "Bob", right...that's what i want to store...just the name (evt 0)
= :create-user
the db is getting updated correctly with the button press, just the [:p "User value: >" (some-> @user :user) "<"]
isn't showing the name... like the component isn't getting re-rendered...
You need a function 2 component I think it’s called? Subscribe and then return a function rather than just the hiccup
@dpsutton seems I dont...just need to deref the subscription and form1 should re-render...
sorry. was using phone. you reference the username with (some-> user :user)
but create it as just (assoc db :user "Bob")
Hi!
Is it OK to subscribe
and deref
inside a reg-sub
? (Or should I stick with reg-sub-raw
?)
The observed behaviour appears to be equivalent, but I'm unsure re: performance/deduplication/caching (or some conceptual mismatch).
(rf/reg-sub-raw
:sub-a
(fn [_ _]
(rr/reaction
(let [parameter @(rf/subscribe [:parameter])]
@(rf/subscribe [:final-sub {:parameter parameter}])))))
(rf/reg-sub
:sub-b
:<- [:parameter]
(fn [[parameter]]
@(rf/subscribe [:final-sub {:parameter parameter}])))
There is another arity to subscribe
which will let you pass in a reaction/subscription, see this slightly outdated tutorial https://github.com/day8/re-frame/wiki/Dynamic-Subscriptions
Then you could write it as
(re-frame/subscribe [:final-sub] [(re-frame/subscribe [:parameter])])
The code you wrote will probably perform fine but you might find the reaction from final-sub
is not bubbling the value up, because the other subs are only running when parameter
changes
Why? The other sub would change if its computation function returns another value as well.
Computation functions only run if their input signals change - either the other subs they have a declared dependency on, or the db by default. Because the final-sub dependency is not declared to re-frame I don't think it would know when it has changed, and therefore not run the computation for sub-b