簡體   English   中英

React-Leaflet:未捕獲的TypeError:pointToLayer不是一個函數

[英]React-Leaflet: Uncaught TypeError: pointToLayer is not a function

我正在嘗試在GeoJSON組件中使用pointToLayer參數。 我在組件(不同文件)之外定義了函數pointToLayer,並且遇到了該錯誤。

Map.js:

import {pointToLayer} from '../helpers/helper-country-point';
<GeoJSON>
      key={_.uniqueId()}
      data={this.props.activeCountry.geojson}
      pointToLayer={pointToLayer(this)}
 ></GeoJSON>

文件../helpers/helper-country-point是:

import L from 'leaflet';
export function pointToLayer(feature, latlng) {
  return L.circleMarker(latlng, {
    color: 'white',
    fillColor: 'white',
    fillOpacity: .8,
    radius: 3,
    stroke: false
  }).bindPopup("MESSAGE") // Change marker to circle
}

當我在Map.js中定義pointToLayer並使用時:

<GeoJSON
 key={_.uniqueId()}
 data= {this.props.countrySelected.geojson}
 pointToLayer={this.pointToLayer.bind(this)}
 ></GeoJSON>

有用。 知道為什么我會不斷收到此錯誤嗎?

在第一個中,您要調用該函數並將this用作參數。

在第二個中,您不調用該功能。 您只是將函數作為道具傳遞。 您可以嘗試在第一個示例中刪除(this)部分,以便僅傳遞函數而不調用它。

然后,由於錯誤是pointToLayer而不是一個函數,並且在map.js聲明該函數時已解決,因此我懷疑您可能將錯誤的路徑寫入了helper.js文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM