[英]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.