diff --git a/reagent-reitit-demo/resources/public/documents/reagent-reitit.org b/reagent-reitit-demo/resources/public/documents/reagent-reitit.org index 7c1ea6a..b023b6f 100644 --- a/reagent-reitit-demo/resources/public/documents/reagent-reitit.org +++ b/reagent-reitit-demo/resources/public/documents/reagent-reitit.org @@ -1,7 +1,4 @@ -#+TITLE: Minimal clojurescript reagent reitit example - - - +#+TITLE: Minimal clojurescript reagent example * Components Reagent allow multiple ways to create components with increasing complexity. @@ -16,7 +13,7 @@ In the example below the function just returns some hiccup with the parameters i (defn navbar-link [{:keys [href title text] :or {text nil title nil}}] [:a.link.dim.dib.mr3 {:key href :href href :title title} text]) -(navbar-link {:href "https://clojure.org" :title "linke title" :text "link here"}) +[navbar-link {:href "https://clojure.org" :title "linke title" :text "link here"}] #+END_SRC @@ -31,21 +28,26 @@ In the example below the function just returns some hiccup with the parameters i [:div.dtc.tr [:h2.f5.mv0 amount]]] [:p.f6.lh-copy.measure.mt2.mid-gray description]]]) -(product-card +[:div.flex + [product-card {:title "Cat 01" :amount "£54.59" - :description "Cat description here" - :link "http://placekitten.com/g/600/300"}) + :description "Cat 1 description here" + :link "http://placekitten.com/g/600/300"}] + [product-card + {:title "Cat 02" + :amount "£34.59" + :description "Cat 2 description here" + :link "http://placekitten.com/g/600/300"}]] #+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 clojure -(defn my-component [title value] - (let [local-state (reagent/atom value)] +(defn my-component [title starting-value] + (let [local-state (reagent/atom starting-value)] (fn [title] [:h1 {:class (when @local-state "hide") :on-click (fn [] (swap! local-state inc))} @@ -54,13 +56,11 @@ In form two we can track local state inside a component a click counter being a [my-component "Clickable component" 1] #+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)] @@ -75,47 +75,18 @@ usually this form of component is only needed when rendering graphics or things [:pre [:code (pr-str info)]])))}))) #+END_SRC +* Fetching a html element reference +If we wish to capture a node we can use =:ref= and store the result in an atom, we can then de reference the atom and call a method on the node using =aget=. -* 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. +#+BEGIN_SRC clojurescript :results output +(defn example-ref-component [title] + (let [local-ref (reagent/atom nil)] + (fn [title] + [:div#example-ref-id.flex.items-center.justify-center.pa4.bg-lightest-blue.navy + {:ref #(reset! local-ref %)} + (str title (when @local-ref (aget @local-ref "id")))]))) -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"] +[example-ref-component "Grabbing the element id using ref "] #+END_SRC diff --git a/reagent-reitit-demo/resources/public/index.html b/reagent-reitit-demo/resources/public/index.html index bffd533..7ab7969 100644 --- a/reagent-reitit-demo/resources/public/index.html +++ b/reagent-reitit-demo/resources/public/index.html @@ -11,7 +11,6 @@ -