This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-15
Channels
- # aleph (5)
- # bangalore-clj (1)
- # beginners (53)
- # boot (1)
- # cljs-dev (327)
- # cljsrn (3)
- # clojure (16)
- # clojure-filipino (47)
- # clojure-nlp (4)
- # clojure-russia (1)
- # clojure-spec (3)
- # clojurescript (64)
- # core-async (6)
- # datomic (25)
- # hoplon (5)
- # jobs (5)
- # klipse (2)
- # lein-figwheel (2)
- # lumo (27)
- # om (2)
- # onyx (6)
- # parinfer (4)
- # pedestal (1)
- # protorepl (1)
- # re-frame (31)
- # ring-swagger (1)
- # specter (9)
- # unrepl (11)
Hi, I've been banging my head against the wall trying to get an input's atom updated when a subscription changes. Has anyone tried that before?
I have something along these lines:
(defn foo []
(let [bar (rf/subscribe [:bar])
value (r/atom @bar)]
(fn []
[:input {:value @value
:on-change ...
I understand in the code above why value
doesn't get updated when the subscription is updated. I just can't figure out a way to accomplish what I need: to update value
when bar
is updated.@ccidral you could try using r/run!
:
(defn foo []
(let [bar (rf/subscribe [:bar])
value (r/atom nil)]
(r/run! (reset! value @bar))
(fn []
[:input {:value @value
:on-change ...
it requires (:require-macros [reagent.ratom :refer [run!]])
edited: removed initial value in atom, because run!
is executed immediately
holy cow thanks @metametadata it works like a charm
patterns like this (adding watches to stuff) are prone to memory leaks because it's easy to create circular references
but it's hard to predict for me if this particular one has this problem
well, React components are stateful and are free to manage their own state, so it's looks ok to me
maybe I would think about moving this "last change wins" logic into the event handler instead of keeping it in the component, if possible
because it looks a bit unusual
but I can't tell exactly why I don't like it 🙂
in my case bar
is updated by an event handler, and I have to update the input's value with bar
's value
I see. What I meant was, unless there's some additional trickery in the component, ideally ratom is not need and component can only depend on the sub:
(defn foo []
(let [bar (rf/subscribe [:bar])]
(fn []
[:input {:value @bar
:on-change (event-that-changes-bar)
oh I see ... in this particular case bar
should change only in a particular moment (ie: not immediately as the user changes the input's value)
it would be nice if there was a function that binds a subscription to a ratom and propagates changes from the former to the latter
This also works:
(defn bind [src target]
(add-watch src
:rf-binding
(fn [_ _ _ value]
(reset! target value))))
Then
(bind bar value)
@ccidral @metametadata I've recently experimented with something similar: https://gist.github.com/pesterhazy/bc309afa0883f29a07131685cc1087da
there's a klipse live demo if you scroll to the bottom
here's the demo: http://app.klipse.tech/?container=1&cljs_in.gist=pesterhazy/0f350623f871140e2fffbb8415536f21
there's also r/track!
for adding side effects to state changes
Thanks a lot @pesterhazy