Skip to content

Instantly share code, notes, and snippets.

@athomasoriginal
Created April 15, 2020 01:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athomasoriginal/eadc022482c3432943c400cc8eeb1788 to your computer and use it in GitHub Desktop.
Save athomasoriginal/eadc022482c3432943c400cc8eeb1788 to your computer and use it in GitHub Desktop.
reitit routing clojurescript
(ns ^:figwheel-hooks reitit-routing
(:require
[reagent.core :as r]
[reagent.dom :as r.dom]
[reitit.frontend :as rf]
[reitit.frontend.easy :as rfe]
[reitit.coercion.spec :as rss]
[fipp.edn :as fedn]))
;; State
;; -----------------------------------------------------------------------------
(defonce match (r/atom nil))
(def about-screen-state (r/atom 0))
(defn increment-click!
[state]
(swap! state inc))
;; Components
;; -----------------------------------------------------------------------------
(defn screen-clicks
[click-count]
[:p (str "page count: " click-count)])
(defn home-page []
(let [state (r/atom 0)]
(fn []
[:div
[:h1 "Homepage"]
(println {:hello "hi"})
(js/console.log {:hello "hi"})
[:button
{:type "button"
:on-click #(rfe/push-state ::about)}
"About"]
[:div
[:p "Welcome to the home page"]
[screen-clicks @state]
[:button
{:on-click #(increment-click! state)}
"Count for Glory!"]]])))
(defn about-page []
(let []
(fn []
[:div
[:h1 "About"]
[:button
{:type "button"
:on-click #(rfe/push-state ::homepage)}
"Homepage"]
[:div]
[:p "Welcome to the about page"]
[screen-clicks @about-screen-state]
[:button
{:on-click #(increment-click! about-screen-state)}
"Count for Glory!"]])))
(defn current-page []
[:div
[:ul
[:li [:a {:href (rfe/href ::homepage)} "Homepage"]]
[:li [:a {:href (rfe/href ::about)} "About"]]]
(if @match
(let [view (:view (:data @match))]
[view @match]))])
;; Router
;; -----------------------------------------------------------------------------
(def routes
[["/"
{:name ::homepage
:view home-page}]
["/about"
{:name ::about
:view about-page}]])
(defn init! []
(rfe/start!
(rf/router routes {:data {:coercion rss/coercion}})
(fn [m] (reset! match m))
;; set to false to enable HistoryAPI
{:use-fragment true})
(r.dom/render [current-page] (.getElementById js/document "root")))
(init!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment