This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-21
Channels
- # announcements (39)
- # architecture (7)
- # aws (9)
- # babashka (111)
- # beginners (139)
- # bristol-clojurians (1)
- # calva (47)
- # chlorine-clover (5)
- # cider (17)
- # clj-kondo (26)
- # clojars (25)
- # clojure (251)
- # clojure-berlin (1)
- # clojure-dev (5)
- # clojure-europe (22)
- # clojure-france (1)
- # clojure-hungary (6)
- # clojure-losangeles (8)
- # clojure-nl (18)
- # clojure-spec (3)
- # clojure-uk (68)
- # clojured (32)
- # clojurescript (32)
- # core-async (10)
- # core-typed (120)
- # cursive (8)
- # datascript (10)
- # datomic (11)
- # docker (2)
- # emacs (6)
- # figwheel-main (4)
- # fulcro (10)
- # graalvm (92)
- # hoplon (2)
- # instaparse (9)
- # jobs (3)
- # jobs-discuss (31)
- # joker (2)
- # kaocha (1)
- # lambdaisland (5)
- # leiningen (10)
- # luminus (1)
- # lumo (14)
- # meander (30)
- # mid-cities-meetup (1)
- # midje (1)
- # off-topic (46)
- # pathom (22)
- # perun (2)
- # re-frame (10)
- # reitit (1)
- # remote-jobs (8)
- # shadow-cljs (71)
- # spacemacs (7)
- # sql (40)
- # tools-deps (31)
- # tree-sitter (11)
- # vim (14)
- # vscode (2)
- # xtdb (5)
@andrea.crotti https://github.com/gadfly361/breaking-point is a nice and simple impl for re-frame
Hi! I'm working on a very basic chat app with re-frame, but I'm having trouble understanding how to treat side effects. I have the most basic use case for a chat box: Whenever a new message is sent, the scroll box must scroll to the bottom (to simplify, let's say this is done always). So I implemented an event (the message being sent) and an effect (the box scrolling to the bottom). The event triggers the aforementioned effect:
(rf/reg-fx
::scroll-chat-to-bottom
(fn [value]
(when value
(let [msg-history (-> js/document (.getElementById "message-history"))]
(set! (.-scrollTop msg-history) (.-scrollHeight msg-history))))))
(rf/reg-event-fx
::send-chat-message
(fn-traced [{db :db} [_ msg]]
{:db (update db :chat-messages conj msg)
::scroll-chat-to-bottom true}))
Now, this has a bug: Whenever a message is sent, the chatbox is first scrolled, and then the db is updated, which triggers a view update. Because the scroll is set first, the chatbox will always be at the second to last message.
So, I guess I need a way to trigger an effect after the view has updated. Is there a way I can do this?
I have the feeling I'm not modelling this the "re-frame" way, so please feel free to correct my overall approach on this.So, I'd state the problem as ...
What you have at the moment is a side effect called scroll-right-now
But what you need is instead a side effect which is scroll-soon-but-after-the-next-render
So the only problem you have is in writing this better side effect ...
Perhaps use https://github.com/reagent-project/reagent/blob/master/src/reagent/core.cljs#L336
Probably a bug in this ... but something like ...
(rf/reg-fx
::scroll-chat-to-bottom
(fn [value]
(when value
(reagent/after-render
(fn []
(let [msg-history (-> js/document (.getElementById "message-history"))]
(set! (.-scrollTop msg-history) (.-scrollHeight msg-history)))))))