#+TITLE: Minimal clojurescript reagent reitit example https://github.com/reagent-project/reagent https://purelyfunctional.tv/guide/reagent/#what-is-reagent https://github.com/metosin/reitit https://www.metosin.fi/blog/reitit/ * Figwheel Main https://figwheel.org/ https://figwheel.org/docs/hot_reloading.html Figwheel is a tool that does hot reloading of your code, it greatly simplifys the tooling required to develop and deploy your applications. You can control how reloading works by using meta data in your project, usually adding it as a tag on your main namespace, and hooking your main function which calls =reagent/render= #+BEGIN_SRC clojurescript ;;(require '[reagent.core :as r]) (ns ^:figwheel-hooks core.demo) #+END_SRC #+BEGIN_SRC clojurescript (defn ^:after-load render-site []) #+END_SRC * Components Reagent allow multiple ways to create components with increasing complexity. There is some good info in this article. https://purelyfunctional.tv/guide/reagent/ ** Form 1 components Form one components are for simply rendering some html with values that are not going to change. In the example below the function just returns some hiccup with the parameters inserted, you need to specify =:key= when dynamically repeating the elements and these should be reproducible unique id's where possible not randomly generated or indexed numbers if the data is unordered. #+BEGIN_SRC clojurescript (defn navbar-link [{:keys [href title text] :or {text nil title nil}}] [:a.link.dim.white.dib.mr3 {:key href :href href :title title} text]) #+END_SRC ** Form 2 components In form two we can track local state inside a component a click counter being a basic example. #+BEGIN_SRC clojurescript #+END_SRC ** Form 3 components This form of component give's you full access to the react life cycle methods, so render did-mount did-unmount etc usually this form of component is only needed when rendering graphics or things like graphs, it's also useful for capturing errors and handling them as in the example below, which renders your components but if =component-did-catch= is trigger the error is caught and displayed instead. #+BEGIN_SRC clojurescript ;;(require '[reagent.core :as reagent]) ;;(require '[reitit.frontend.easy :as rfe]) (defn err-boundary [& children] (let [err-state (reagent/atom nil)] (reagent/create-class {:display-name "ErrBoundary" :component-did-catch (fn [err info] (reset! err-state [err info])) :reagent-render (fn [& children] (if (nil? @err-state) (into [:<>] children) (let [[_ info] @err-state] [:pre [:code (pr-str info)]])))}))) #+END_SRC * Routing Routing with reitit is all about data, you store your routes as nested vectors of hash maps. the hash map should take a name and view param at least but you can add in params and validate the data. Reitit works as a backend and frontend routing library so you can share routes between the two. These are a few simple routes, the last takes parameters and does validation checking against the values. #+BEGIN_SRC clojurescript (def routes [["/" {:name ::frontpage :view 'home-page-function}] ["/about" {:name ::about :view 'about-page-function}] ["/item/:id" {:name ::item :view 'item-page-function :parameters {:path {:id int?} :query {:foo keyword?}}}]]) #+END_SRC You need to connect your routes data structure to =ref/start!= this function take's your own function where you can handle what should happen on route change, in this example an atom is updated causing react to render the new page. #+BEGIN_SRC clojurescript (def site-state (reagent/atom nil)) (rfe/start! (rf/router routes {:data {:coercion rss/coercion}}) (fn [m] (swap! site-state assoc :current-route m)) ;; set to false to enable HistoryAPI {:use-fragment true}) #+END_SRC To create a link to a route, you can use the =rfe/href= function which takes a lookup key which you specified in your routes, in this instance the key is name spaced to the current namespace. #+BEGIN_SRC clojurescript [:a {:href (rfe/href ::frontpage)} "example link"] #+END_SRC