Using React Native MapView in ClojureScript Reagent

有些话、适合烂在心里 提交于 2021-02-06 13:50:50

问题


I am trying to use MapView from https://github.com/airbnb/react-native-maps using Reagent. It works fine but how could I get local state for MapView when onRegionChange event is fired? Trying to use current/component but it is always nil.

(def Expo (js/require "expo"))
(def map-view (r/adapt-react-class (.-MapView Expo)))

(defn my-map []
   (r/create-class
     {:component-did-mount (fn [this] 
                       (println "component mount "))
      :reagent-render (fn []
                  (let [this (r/current-component)]
                    [map-view {:style {:flex 1}
                               :initialRegion {:latitude 37.78825
                                               :longitude -122.4324
                                               :latitudeDelta 0.0922
                                               :longitudeDelta 0.0421}
                                 :onRegionChange (fn []
                                                  ;; Would like to see state here.
                                                  (println (.. this -state))                                                       )}]))}))

回答1:


Region information

The onRegionChange callback has Region as argument. Region has the following signature:

type Region {
  latitude: Number,
  longitude: Number,
  latitudeDelta: Number,
  longitudeDelta: Number,
}

You can get the values from the Region by using goog.object/get.

If you get the region and extract the values from it your my-map looks like:

(defn my-map []
  [map-view {:style         {:flex 1}
             :initialRegion {:latitude       52.3702
                             :longitude      4.89516
                             :latitudeDelta  0.0922
                             :longitudeDelta 0.0421}
             :onRegionChange (fn [region]
                               (alert (str "Latitude: "
                                           (goog.object/get region "latitude")
                                           "\nLongitude: "
                                           (goog.object/get region "longitude"))))}])

You can obtain the latitudeDelta and longitudeDelta in the same manner.

When you drag the map the latitude and longitude show up:

react-native-maps

The component

If you want access to the component itself your code works fine, you just have to visualize it:

(defn jsx->clj
  [x]
  (into {} (for [k (.keys js/Object x)] [k (aget x k)])))

(defn my-map []
  (let [this (r/current-component)]
    [map-view {:style          {:flex 1}
               :initialRegion  {:latitude       37.78825
                                :longitude      -122.4324
                                :latitudeDelta  0.0922
                                :longitudeDelta 0.0421}
               :onRegionChange (fn [region]
                                 ;; Would like to see state here.

                                 (alert "The component..."
                                        (str (jsx->clj this))))}]))

This prints something like:

Print component itself

Not sure if you can do anything with the component, I think you need the Region information.



来源:https://stackoverflow.com/questions/46298865/using-react-native-mapview-in-clojurescript-reagent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!