Skip to content
Browse files
+fulcro RN
  • Loading branch information
souenzzo committed Mar 4, 2019
1 parent 217f4ca commit 3ef0602de20210e9c29942c2580cd68e7f0958b9
Showing with 125 additions and 2 deletions.
  1. +89 −2 src/rn/app_native/rn.cljs
  2. +36 −0 src/rn/app_native/rn_support.cljs
@@ -1,7 +1,94 @@
(ns app-native.rn
(:require ["react-native" :as rn]
["react" :as r]))
["react" :as r]
[app-native.rn-support :as rn-support]
[fulcro.client :as fc]
[goog.dom :as gdom]
[fulcro.client.primitives :as fp]
[fulcro.client.routing :as fr]
[fulcro.client.mutations :as fm]
[ :as net]
[goog.object :as gobj]
[ :as df]))

(defn Text
[props & childs]
(apply r/createElement rn/Text props childs))

(defn Button
[props & childs]
(apply r/createElement rn/Button props childs))

(defn View
[props & childs]
(apply r/createElement rn/View props childs))

(fp/defsc CounterComp [this {:app.counter/keys [value id]}]
{:query [:app.counter/id
:ident [:app.counter/id :app.counter/id]}
#js {}
(Text #js {} (pr-str value))
(Button #js {:disabled (not (number? value))
:title "Inc"
:onClick #(fp/transact! this `[(app/inc ~{:app.counter/id id})])})))

(def ui-counter (fp/factory CounterComp {:keyfn :app.counter/id}))

(fm/defmutation app/inc
[{:app.counter/keys [id]}]
(action [{:keys [state]}]
(swap! state update-in [:app.counter/id id :app.counter/value] inc))
(remote [{:keys [ast state]}]
(-> ast
(fm/returning state CounterComp))))

(fp/defsc Home [this {::keys [page id]
:ui/keys [counter]}]
{:query [{:ui/counter (fp/get-query CounterComp)}
:ident (fn [] [page id])
:initial-state (fn [_] {::page ::home
::id ::home})}

#js {}
(Button #js {:title "Refresh"
:onClick #(df/load this :>/counter CounterComp
{:target [::home ::home :ui/counter]})})
(ui-counter counter)))

(fr/defsc-router Router [this {::keys [page id]}]
{:router-targets {::home Home}
:ident (fn [] [page id])
:router-id ::router
:default-route Home}

(def ui-router (fp/factory Router))

(fp/defsc Root [this {::keys [router]}]
{:query [{::router (fp/get-query Router)}]
:initial-state (fn [_]
{::router (fp/get-initial-state Router _)})}
(ui-router router))

(defonce app
(atom (fc/make-fulcro-client
{:reconciler-options {:root-render rn-support/root-render
:root-unmount rn-support/root-unmount}})))

(defonce RootNode (rn-support/root-node! 1))
(defonce ui-root-node (fp/factory RootNode))

(defn reload []
(swap! app fc/mount Root 1))

(defn main
(r/createElement rn/Text #js {} "Hello from cljs"))
(ui-root-node {}))

@@ -0,0 +1,36 @@
(ns app-native.rn-support
(:require [fulcro.client.primitives :as fp]))

(defonce root-nodes (atom {}))

(defn root-node!
"A substitute for a real root node (1) for mounting om-next component.
You have to call function :on-render and :on-unmount in reconciler :root-render :root-unmount function."
(let [content (atom nil)
instance (atom nil)
class (fp/ui Object
(componentWillMount [this] (reset! instance this))
(render [_] @content))]
(swap! root-nodes assoc id {:on-render (fn [el]
(reset! content el)
(when @instance
(.forceUpdate @instance)))
:on-unmount (fn [])
:class class})

(defn root-render
"Use this as reconciler :root-render function."
[el id]
(let [node (get @root-nodes id)
on-render (:on-render node)]
(when on-render (on-render el))))

(defn root-unmount
"Use this as reconciler :root-unmount function."
(let [node (get @root-nodes id)
unmount-fn (:on-unmount node)]
(when unmount-fn (unmount-fn))))

0 comments on commit 3ef0602

Please sign in to comment.