-
-
Save jeans11/41951eaff256d8aecc228e717c9000b3 to your computer and use it in GitHub Desktop.
Routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns demo.fulcro | |
(:require [accountant.core :as history])) | |
;; Fulcro application state | |
(defonce app (app/fulcro-app)) | |
(defn start-routing! [] | |
(history/configure-navigation! | |
{:nav-handler (fn [path] | |
(let [route-segment (vec (rest (str/split path #"/")))] | |
(dr/change-route! app (if (seq route-segment) route-segment [""]))))}) | |
(history/dispatch-current!)) | |
(defn set-root! | |
[app root-component] | |
(swap! (::app/state-atom app) #(merge {:fulcro.inspect.core/app-id (comp/component-name root-component)} %)) | |
(app/set-root! app root-component {:initialize-state? true})) | |
(defn -mount-app! [] | |
(set-root! app entry/Root) | |
(rn/register-root-component! app-name entry/Root app) | |
(start-routing!) | |
(rn/run-application! app-name "app")) | |
(def ui-layout (comp/factory Layout)) | |
(defsc Home [_this _props] | |
{:ident (fn [] [:component/id ::home]) | |
:query [] | |
:route-segment ["home"] | |
:initial-state {}} | |
(rn/ui-view | |
(rn/ui-text "Home"))) | |
(defrouter Router [_this {:keys [current-state]}] | |
{:router-targets [Home]} | |
(prn current-state) | |
(case current-state | |
:initial (rn/ui-text "Show something") | |
:pending (rn/ui-text "Loading...") | |
(rn/ui-text "No route selected"))) | |
(def ui-router (comp/factory Router)) | |
(defsc Root [this {:root/keys [router]}] | |
{:query [{:root/router (comp/get-query Router)}] | |
:componentDidMount (fn [this] (mroot/register-root! this {:app app})) | |
:componentWillUnmount (fn [this] (mroot/deregister-root! this {:app app})) | |
:initial-state {:root/router {}}} | |
(ui-layout {} (ui-router router))) | |
(-mount-app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment