【问题标题】:How to import react-native-maps to Clojruescript Expo project如何将 react-native-maps 导入 Clojruescript Expo 项目
【发布时间】:2020-03-24 13:54:44
【问题描述】:

我正在尝试使用 Expo 和 Clojurescript 创建一个简单的地图应用程序,但是当我尝试渲染地图时出现以下错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

这就是代码的样子:

(ns map-test.core
  (:require [reagent.core :as r :refer [atom]]
            [re-frame.core :refer [subscribe dispatch dispatch-sync]]
            [oops.core :refer [ocall]]
            [map-test.handlers]
            [map-test.subs]))

(def ReactNative (js/require "react-native"))
(def expo (js/require "expo"))
(def view (r/adapt-react-class (.-View ReactNative)))
(def text (r/adapt-react-class (.-Text ReactNative)))
(def MapView (js/require "react-native-maps"))
(def map-view (r/adapt-react-class MapView))

(defn app-root []
  [view {:style {:flex 1}}
   [map-view]])

(defn init []
  (dispatch-sync [:initialize-db])
  (ocall expo "registerRootComponent" (r/reactify-component app-root)))

这是我的package.json

{
  "name": "map-test",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "main": "main.js",
  "dependencies": {
    "expo": "^34.0.3",
    "react": "16.8.3",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "create-react-class": "15.6.3",
    "react-native-maps": "~0.24.0"
  }
}

我尝试按照此处显示的答案进行操作:Using React Native MapView in ClojureScript Reagent,但它似乎不起作用。

感谢所有帮助!

【问题讨论】:

    标签: react-native expo clojurescript react-native-maps reagent


    【解决方案1】:

    如果你控制台记录 react-native-maps 对象,你可以看到 MapView 是 分配给默认

    (.log js/console ReactNativeMaps)
    

    所以你必须要求这样的库:

    (def ReactNativeMaps (js/require "react-native-maps"))
    (def map-view (r/adapt-react-class (.-default ReactNativeMaps)))
    

    这是我的工作测试(core.cljs 文件):

    (ns map-test.core
        (:require [reagent.core :as r :refer [atom]]
                  [re-frame.core :refer [subscribe dispatch dispatch-sync]]
                  [oops.core :refer [ocall]]
                  [myapp.handlers]
                  [myapp.subs]))
    
    (def ReactNative (js/require "react-native"))
    (def expo (js/require "expo"))
    
    (def text (r/adapt-react-class (.-Text ReactNative)))
    (def view (r/adapt-react-class (.-View ReactNative)))
    
    (def ReactNativeMaps (js/require "react-native-maps"))
    (def map-view (r/adapt-react-class (.-default ReactNativeMaps)))
    
    (def styles {:container
                 {:flex 1
                  :alignItems "center"
                  :justifyContent "center"}
                 :mapStyle
                 {:width 300
                  :height 400}})
    
    (defn app-root []
      [view (:container styles)
        [map-view (:mapStyle styles)]])
    
    (defn init []
      (dispatch-sync [:initialize-db])
      (ocall expo "registerRootComponent" (r/reactify-component app-root)))
    

    【讨论】:

    • 无法让您的代码运行,但我明白了,现在我的应用程序可以运行了。万分感谢!
    • 完美!我的荣幸。
    猜你喜欢
    • 2018-12-23
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多