This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-19
Channels
- # aws (2)
- # beginners (135)
- # boot (20)
- # chestnut (7)
- # cider (18)
- # clara (5)
- # cljs-dev (50)
- # cljsrn (30)
- # clojure (252)
- # clojure-italy (9)
- # clojure-losangeles (5)
- # clojure-russia (8)
- # clojure-spec (33)
- # clojure-uk (5)
- # clojurescript (32)
- # clr (4)
- # cursive (5)
- # data-science (1)
- # datascript (1)
- # datomic (40)
- # emacs (1)
- # fulcro (18)
- # graphql (11)
- # hoplon (3)
- # lein-figwheel (2)
- # lumo (47)
- # off-topic (2)
- # om-next (3)
- # onyx (10)
- # pedestal (22)
- # protorepl (6)
- # re-frame (7)
- # reagent (38)
- # ring (1)
- # ring-swagger (5)
- # rum (3)
- # spacemacs (19)
- # specter (5)
- # vim (13)
- # yada (16)
if I assoc the same value under the same path twice to a ratom, will a subsciption deref fire twice?
How would you add the :key
metadata to all elements of the seq with something like (interpose [:br] (string/split str #"\n"))
?
This is just for a temporary message, a random unique key would be good enough.
@l1sp3r looks like @carly is right, but I'm wondering why that is
if you reset!
an atom to the same value, do the watches not fire?
They should not fire because, as far as I understand, it should compare and only fire if there is a difference in the atom.
it’s because react knows to not re-render if the data has not changed, and :key helps it know the item moved in the list (maybe) but did not change
@noisesmith are you replying to me?
the object identity of the item inside the atom is the first thing reagent checks, and then the next thing is value equality, the :key metadata tells it what to check, but even if the object identity is different and the values are equal it should not re-render
not sure I follow
my understanding is that reagent uses forceUpdate manually to trigger rerenders
it doesn't rely on props or this.state
so I'm not sure where a key
would be relevant
@carly do you have a pointer to the source? I can't find the place in reagent where this check is done
so if you start with [:div ^{:key :a} foo ^{:key :b} bar]
and then switch to [:div ^{:key :a} foo ^{:key :c} baz ^{:key :b} bar]
reagent knows to move bar, rather than re-rendering both baz and bar
because based on :key it knows which specific item in the list that was (despite th re-order)
that wasn't @l1sp3r's question though if I understood him - it was about re-renders triggered by resetting a ratom with the same value
@pesterhazy thats right, consensus appear that it doesn't caase a re-render
that’s the other part of what I said: “the object identity of the item inside the atom is the first thing reagent checks, and then the next thing is value equality”
oh - maybe I’m wrong on this…
in general for atoms a watch triggers on reset! even if the values are equal
yup
=> (let [!x (atom {:counter 0}), _ (add-watch !x :test (fn [& args] (prn [:watch args])))] (swap! !x update :counter identity))
[:watch (:test #object [cljs.core.Atom {:val {:counter 0}}] {:counter 0} {:counter 0})]
{:counter 0}
which is a good thing, as otherwise the check would require a deep compare I think
so my theory is that assoc
is smart about returning the same map if oldval==newval
(let [u {:counter 0} v (update u :counter identity)] (identical? u v))
which makes it true that after swap!ing an atom, oldval is still identical to newval
.. and this is checked in the reaction: https://github.com/reagent-project/reagent/blob/master/src/reagent/ratom.cljs#L376
meaning that if the actual values don't change, the re-render is not triggered even though the ratom was dereffed
if that's true, quite a few things have to conspire for that to actually work
here’s my figwheel repl:
(cmd)dev:cljs.user=> (def m (reagent.core/atom {:a 0}))
#'cljs.user/m
(ins)dev:cljs.user=> (def m0 @m)
#'cljs.user/m0
(ins)dev:cljs.user=> (swap! m update :a identity)
{:a 0}
(ins)dev:cljs.user=> (def m1 @m)
#'cljs.user/m1
(ins)dev:cljs.user=> (identical? m0 m1)
true
now if you used reset! you won’t see that kind of result I bet
(ins)dev:cljs.user=> (reset! m {:a 0})
{:a 0}
(ins)dev:cljs.user=> (def m2 @m)
#'cljs.user/m2
(ins)dev:cljs.user=> (= m1 m2)
true
(ins)dev:cljs.user=> (identical? m1 m2)
false
(def app
(create-tiny-app->
{:model store
:updater updater
:view comp-container
:mount-target (.querySelector js/document ".app")
:ssr? false
:show-ops? true}))
(def reload! (:reload! app))
(set! (.-onload js/window) (:start-app! app))
If anyone is interested, read more here https://github.com/Respo/minimal-tiny-app/blob/master/src/app/main.cljs#L37