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
I have (fn [] ...)
in the right places
What is usually the cause of this? How to fix?
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.
Yes that’s the case here too
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
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])))
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])))
On initial page load the active-organization
is nil
But it is fetched and then it should reload
Which it does not
I found the solution!
I added a ^{:key}
Based on the thing that changed later
(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]])))
Then the rerender workd
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.
permission-state creates the initial state that is used for updating and stuff
It needs to be r/atom because other components use the state to update checkboxes and stuff and @state to rerender themselves
It’s basically component local state
Same problem if I put it in app-db, but I think this is bit nicer since it’s local state
Thanks for the help!