reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
jeff tang 2020-04-02T14:36:55.027700Z

Hi! I'm using reitit-frontend within a re-frame project. I'm having trouble getting my subscription for :current-route to update properly when I refresh the page on a not / page, e.g. /#/about

; views.cljs...
(defn main-panel []
  (let [current-route @(subscribe [:current-route])]
    (fn []
      [:div
       [:h1 "Hello World"]
       [:p (str "Current Route: " (-> current-route :data :name))] ;; <-----  :current-route initial value is nil, so this starts as empty string
       ])))

; router.cljs...
(defn on-navigate [new-match]
  (when new-match
    (do (prn "on-navigate ->")
        (cljs.pprint/pprint new-match) ;; <---------------- prints the correct match, db updates current-route, but view is not re-rendered
        (dispatch [:navigated new-match]))))

2020-04-02T14:42:01.029100Z

Might be a React quirk. Try this: before the first [:div in main-panel, put

^{:key (str "main-" (-> current-route :data :name))}

2020-04-02T14:42:27.029600Z

That will give React a kick in the pants to force a re-render of your div.

2020-04-02T14:42:42.029900Z

(I hope :hand_with_index_and_middle_fingers_crossed: )

jeff tang 2020-04-02T14:46:32.030200Z

(defn main-panel []
  (let [current-route @(subscribe [:current-route])]
    (fn []
      ^{:key (str "main-" (-> current-route :data :name))}
      [:div
       [:h1 "Hello World"]
       [:p (str "Current Route: " (-> current-route :data :name))]
       [match-name (-> current-route :data :name)]
       ])))

jeff tang 2020-04-02T14:46:42.030500Z

doesn't seem to have an effect

jeff tang 2020-04-02T14:52:25.030600Z

Using 10-x, I can see that the event is being called and the db updates

2020-04-02T14:56:22.032200Z

Hmm, the route name is changing, right? If so, then the other thing I’d try is moving the ^{:key...} line down to just above the [:p...]

2020-04-02T14:57:56.032800Z

Oh wait, you want @(subscribe...) inside your render function

jeff tang 2020-04-02T14:58:39.033300Z

Ohhhh

2020-04-02T14:58:51.033600Z

Shouldn’t need the ^{:key...} trick at all, just move the subscribe deref.

2020-04-02T14:59:27.034100Z

The subscribe can be in an outer let, but the @ has to be inside the fn.

jeff tang 2020-04-02T15:00:15.034600Z

Thank you so much

jeff tang 2020-04-02T15:02:24.036500Z

I had a hunch it was a layer-2 vs layer-1 but the deref was a lot more subtle

jeff tang 2020-04-02T15:02:33.036700Z

thank you @manutter51

👍 1
2020-04-02T15:03:47.037500Z

I’d have picked up on that quicker but I had to go off coffee for health reasons 😕

🍵 2
oly 2020-04-02T15:59:58.039500Z

is there a way to get a multi select, {1: "select one"} {2: "select two"} so you can select one or two or both giving you a list of [1] or [1,2].

oly 2020-04-02T16:00:31.040Z

I have been looking in spec tools, but have not found what I am looking for