re-frame

https://github.com/Day8/re-frame/blob/master/docs/README.md https://github.com/Day8/re-frame/blob/master/docs/External-Resources.md
erwinrooijakkers 2020-08-28T17:10:28.029600Z

I am having a situation where my initial page load does work when I use save a page once in shadow-cljs and it rerenders, but not on initial load

erwinrooijakkers 2020-08-28T17:10:47.030100Z

I have (fn [] ...) in the right places

erwinrooijakkers 2020-08-28T17:10:59.030300Z

What is usually the cause of this? How to fix?

p-himik 2020-08-28T17:18:15.030700Z

I've encountered this some time ago. As far as I recall, it was a timing issue. I.e. I was expecting something to be ready by the moment the rendering is started, but it was not.

erwinrooijakkers 2020-08-28T17:22:23.030900Z

Yes that’s the case here too

erwinrooijakkers 2020-08-28T17:23:39.031100Z

I have something like this:

(defn container [user-info organization-paths] (let [path (re-frame/subscribe [:permission-panel/active-organization-path]) organization (re-frame/subscribe [:permission-panel/active-organization])] (fn [] [:div.ml-6 [permission-panel @path user-info @organization]])))``` I expect permission-panel to rerender when there’s a new @organization

erwinrooijakkers 2020-08-28T17:24:54.031300Z

And I create a Reagent state from these contents that I update:

(defn permission-panel [path user-info organization]
  (let [state (r/atom (permission-state user-info organization))]
    (fn []
      [history-block @state])))

erwinrooijakkers 2020-08-28T17:25:14.031500Z

With sub like this:

(re-frame/reg-sub
 :permission-panel/active-organization
 (fn [{:permission-panel/keys [active-organization-path] :as db}]
   (get-in db [:organisatie/organizations active-organization-path])))

erwinrooijakkers 2020-08-28T17:25:24.031700Z

On initial page load the active-organization is nil

erwinrooijakkers 2020-08-28T17:25:29.031900Z

But it is fetched and then it should reload

erwinrooijakkers 2020-08-28T17:25:34.032100Z

Which it does not

erwinrooijakkers 2020-08-28T18:04:47.032600Z

I found the solution!

erwinrooijakkers 2020-08-28T18:05:12.033100Z

I added a ^{:key}

erwinrooijakkers 2020-08-28T18:05:27.033400Z

Based on the thing that changed later

erwinrooijakkers 2020-08-28T18:06:52.034Z

(defn container [user-info organization-paths]
  (let [path (re-frame/subscribe [:permission-panel/active-organization-path])
        organization (re-frame/subscribe [:permission-panel/active-organization])]
    (fn []
      [:div.ml-6
       ^{:key (:some-value @organization)}
       [permission-panel @path user-info @organization]])))

erwinrooijakkers 2020-08-28T18:07:03.034200Z

Then the rerender workd

p-himik 2020-08-28T18:08:30.034300Z

Why do you wrap (permission-state ...) in an atom in the first place? Just use it directly, and there won't be the need for the :key metadata.

erwinrooijakkers 2020-08-28T18:56:16.034500Z

permission-state creates the initial state that is used for updating and stuff

erwinrooijakkers 2020-08-28T18:56:32.034700Z

It needs to be r/atom because other components use the state to update checkboxes and stuff and @state to rerender themselves

erwinrooijakkers 2020-08-28T18:56:49.034900Z

It’s basically component local state

erwinrooijakkers 2020-08-28T18:57:13.035100Z

Same problem if I put it in app-db, but I think this is bit nicer since it’s local state

erwinrooijakkers 2020-08-28T18:57:17.035300Z

Thanks for the help!