Fork me on GitHub
#reitit
<
2019-12-06
>
gekkostate17:12:55

Hi everyone! Quick question about query parameters in reitit router. I'm using reitit.core/router as my router. I would like to access parameters in my path for example: /bar?great=foo. Currently, I have my router setup like the following

(def router
  (reitit/router
   [["/" :home]
    ["/search" :search]]))
What is the way to access query parameters?

ikitommi17:12:42

@gekkostate the core router is unaware of query parameters. If you are routing in the browser, you should check reitit.frontend/router which also parses query-params. Or reitit.ring/router for the same for backend

gekkostate17:12:06

Ohhhh ok! Cool, I'll check that out. I've been fiddling with the luminus template which contains reagent + re-frame and reitit (frontend is the core router and backend is ring/router). They have an interesting setup with the core router but I'm going to shift to the reitit.frontend/router because that seems to be the best way forward. Thanks a bunch for your help!

alekszelark17:12:40

@gekkostate here is an example

(def router (reitit.frontend/router ["/" ["search"]]))

(reitit.frontend/match-by-path router "/search?q=foo")
=> #reitit.core.Match{:template "/search", :data {}, :result nil, :path-params {}, :path "/search", :query-params {:q "foo"}, :parameters {:path {}, :query {:q "foo"}}}

gekkostate19:12:57

Thanks a bunch, the example really helped! Quick question. Is there a standard way to pass those params onto the view? I'm following this: https://github.com/metosin/reitit/blob/master/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs. My attempt was to subscribe to ::current-route which passes the entire match to the view. However, that means that routes which have nothing to do with subscribed view are also passed and that doesn't look like the good approach.

(defn sub-page2
  []
  (let [params @(re-frame/subscribe [::current-route])]
    (println params)
    [:div
     [:h1 "This is sub-page 2"]]))