【问题标题】:How to use StackNavigator of React Navigation in clojurescript如何在clojurescript中使用React Navigation的StackNavigator
【发布时间】:2017-04-01 05:10:13
【问题描述】:

我是clojurescript 和试剂的新手。我尝试在我的 react-native 应用程序中使用 react-navigation 但我收到此错误

Error rendering component (in env.main.reloader > exp_cljs.core.app_root > reagent2)

这是我的代码

(def react-navigation (js/require "react-navigation"))
(def StackNavigator (aget react-navigation "StackNavigator"))

(defn Home
  []
  [text "Hello Navigator"])

(defn SimpleApp
  []
  (StackNavigator
    (clj->js {:Home {:screen (r/create-class {:reagent-render (Home)})}})))

(defn init []
  (dispatch-sync [:initialize-db])
  (.registerComponent rn/app-registry "main" #(r/reactify-component app-root)))

这是我的app-root

(defn app-root []
  (SimpleApp)); -- error
  ;(r/create-class {:reagent-render SimpleApp}); error
  ;(r/as-element (SimpleApp)); -- error
  ;(r/adapt-react-class SimpleApp)); --  error

【问题讨论】:

    标签: react-native react-navigation clojurescript cljsrn


    【解决方案1】:
      (ns same.app
      (:require [reagent.core :as r]
                [same.ui :as ui]
                [same.util :as u]
                [same.screens.auth :refer [AuthScreen]]
              ;  [same.screens.reg :refer [RegScreen]]
             ;  [same.screens.resend :refer [ResendScreen]]
                [same.screens.splash :refer [SplashScreen]]
               ; [same.screens.drawer :refer [Drawer]]
                [same.screens.presentation :refer [Presentation]]))
    
    (def routes #js {;:Drawer #js {:screen (r/reactify-component Drawer)}
                     :AuthScreen #js {:screen (r/reactify-component AuthScreen)}
                     ;:RegScreen #js {:screen (r/reactify-component RegScreen)}
                     ;:ResendScreen #js {:screen (r/reactify-component ResendScreen)}
                     :Presentation #js {:screen (r/reactify-component Presentation)}
                     :Splash #js {:screen (r/reactify-component SplashScreen)}})
    
    (def Routing (ui/StackNavigator.
                   routes
                   #js {:initialRouteName "Splash"
                        :headerMode "none"
                        :mode "modal"}))
    
    (def routing (r/adapt-react-class Routing))
    
    (defn AppNavigator []
      (fn []
        [routing]))
    

    和android.core:

    (ns same.android.core
      (:require [reagent.core :as r :refer [atom]]
                [re-frame.core :refer [dispatch-sync]]
                [same.ui :as ui]
                [same.events]
                [same.subs]
                [same.app :refer [AppNavigator]]))
    
    (aset js/console "disableYellowBox" true)
    
    (defn app-root []
      (fn []
        [AppNavigator]))
    
    (defn init []
      (dispatch-sync [:initialize-db])
      (.registerComponent ui/app-registry "Same" #(r/reactify-component app-root)))
    

    【讨论】:

      【解决方案2】:

      诀窍是将 React Native 组件转换为 Reagent 组件,然后在需要的地方返回。在以下示例中,login-screenmain-screen 的定义被省略,它们只是常规的试剂组件。

      (def react-navigation (js/require "react-navigation"))
      (def stack-navigator (.-createStackNavigator react-navigation))
      (def switch-navigator (.-createSwitchNavigator react-navigation))
      ; ...
      ; ...
      ; ...
      (defn application-nav-stack []
        (stack-navigator (clj->js { "MainApp" (r/reactify-component main-screen) })))
      
      (defn authentication-nav-stack []
        (stack-navigator (clj->js { "Login" (r/reactify-component login-screen) })))
      
      (defn app-navigation-switch []
        (switch-navigator
          (clj->js { :Auth (authentication-nav-stack)  :MainApp (application-nav-stack)  })
          (clj->js { :initialRouteName :Auth } )))
      
      (defn app-root []
        [ (r/adapt-react-class (app-navigation-switch)) ] )
      
      (defn init []
        (dispatch-sync [:initialize-db])
        (.registerComponent app-registry "MyApp" #(r/reactify-component app-root)))
      

      每当您将 Reagent 组件传递给 ReactNavigation 函数时,您都需要使用reactify-component 将其转换为原生 JS 表单;每当您需要使用 ReactNavigation 组件作为 Reagent 组件的一部分时,您需要使用adapt-react-class 将其转换回来。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-27
        相关资源
        最近更新 更多