This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-04
Channels
- # announcements (2)
- # beginners (24)
- # cider (53)
- # clara (4)
- # clj-kondo (3)
- # cljdoc (2)
- # clojars (1)
- # clojure (17)
- # clojure-dev (48)
- # clojure-russia (14)
- # clojure-uk (10)
- # clojuredesign-podcast (5)
- # clojurescript (11)
- # cursive (4)
- # events (5)
- # joker (1)
- # juxt (1)
- # kaocha (1)
- # re-frame (13)
- # reagent (1)
- # reitit (2)
- # sql (28)
How can I see or log day8.re-frame.http-fx
request before sending it.
Dear fellow re-frame users, have you ever faced the question of view key management? I initially fed entities ids to reagent as views keys. But when a new entity under a view is saved, it obtains a server ID, so its key changes and its view re-renders losing focus and user’s patience. As a result, I do some unnecessary things with key management. I suspect that I can somehow do better than this.
how it was initially
--- entity created ---
(assoc-in db [:entities client-id] entity)
--- render entity ---
^{:key (:id new-entity)} [view new-entity] ; gives client-id
--- do changes ---
--- persist to server ---
--- update entity id ---
--- render again ---
^{:key (:id entity)} [view entity] ; gives server-id, forces re-render, loses focus
how I re-wrote it
--- entity created ---
(assoc-in db [:entities client-id] entity)
--- add view key ---
(assoc-in db [:ui/main-pane-keys client-id] (str "main-pane-" client-id))
--- render-entity ----
^{:key (get-in db [:ui/main-pane-keys client-id])} [view new-entity] ; gives client-id based key
--- persist to server ---
--- update entity id ---
--- add mapping from server id to view-key ---
--- render again ---
^{:key (get-in db [:ui/main-pane-keys server-id])} [view entity] ; still gives client-id based key, so no re-render, keeps focus
I see! You could create instead a react class like this:
(defn foo [entity]
(r/create-class
{:should-component-update
(fn [this old-props new-props]
;; re-render only when anything but :entites changes
;; the component rerenders only when true is returned )
:reagent-render
(fn [entity]
^{:key (:id entity)}
[view entity])})))
But I don't see any particular problem with your approach either.. I am just giving a different solution you might or might not find more suitable for your specific use case 🙂
Just be careful about this: >We do not recommend doing deep equality checks or using JSON.stringify() in shouldComponentUpdate(). It is very inefficient and will harm performance.
You could do a simple keys
on the diff
and just re-render if count is greater than 1 or if the changing key is not :entities
which interceptor is that?
how it was initially
--- entity created ---
(assoc-in db [:entities client-id] entity)
--- render entity ---
^{:key (:id new-entity)} [view new-entity] ; gives client-id
--- do changes ---
--- persist to server ---
--- update entity id ---
--- render again ---
^{:key (:id entity)} [view entity] ; gives server-id, forces re-render, loses focus
how I re-wrote it
--- entity created ---
(assoc-in db [:entities client-id] entity)
--- add view key ---
(assoc-in db [:ui/main-pane-keys client-id] (str "main-pane-" client-id))
--- render-entity ----
^{:key (get-in db [:ui/main-pane-keys client-id])} [view new-entity] ; gives client-id based key
--- persist to server ---
--- update entity id ---
--- add mapping from server id to view-key ---
--- render again ---
^{:key (get-in db [:ui/main-pane-keys server-id])} [view entity] ; still gives client-id based key, so no re-render, keeps focus