3

I am trying to use reagent to build my very basic project but I have a problem with routing and its parameter. This is from reagent looks like

EDITED - :require s added

(ns hammerslider.core
     (:require [reagent.core :as reagent :refer [atom]]
               [secretary.core :as secretary :include-macros true]
               [accountant.core :as accountant]))     

;; Views

 (defn home-page []
   [:div [:h2 "Welcome to hammerslider"]
    [:div [:a {:href "/c/12"} "go custom"]]])

 (defn c [test]
   [:div [:h2 (str "on C " test)]
    [:div [:a {:href "/"} "go to the home page"]]])

I am trying to get 12 from c route which is the route handling is look like this

 (def page (atom #'home-page))

 (defn current-page []
   [:div [@page]])

 (secretary/defroute "/" []
   (reset! page #'home-page))

 (secretary/defroute "/c/:test" [test]
   (reset! page #'c)

enter image description here

I'm trying to catch the test parameter with the view function but it appears on C, not on C 12. How do I get to transfer the test parameter in to the view of c? or should I save it on different atoms?

EDITED - Mine solved by saving parameters into atom and it works, but is it the right way to pass the parameter?

(def parameter (atom ()))

(defn c []
  [:div [:h2 (str "on C " (:test @parameter))]
   [:div [:a {:href "/"} "go to the home page"]]])

(secretary/defroute "/c/:test" {:as params}
  (do (js/console.log params)
      (reset! parameter params)
      (reset! page #'c)
      ))
Ampersanda
  • 2,016
  • 3
  • 21
  • 36

1 Answers1

1

It is depended on how you use your route parameters. The only guarantee between your program and reagent is if the value in ratom changed, the reagent component will be changed accordingly.

The TodoMVC is quite feature completed example for you to use reagent and secretary.

https://github.com/tastejs/todomvc/blob/gh-pages/examples/reagent/src/cljs/todomvc/routes.cljs

By the way, most of the time I will use re-frame instead of using reagent directly.

ka yu Lai
  • 571
  • 3
  • 13