简体   繁体   English

React-Leaflet:未捕获的TypeError:pointToLayer不是一个函数

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

I'm trying to use the pointToLayer parameter in the GeoJSON component. 我正在尝试在GeoJSON组件中使用pointToLayer参数。 I defined the the function pointToLayer outside of the component ( different file) and I get getting that error. 我在组件(不同文件)之外定义了函数pointToLayer,并且遇到了该错误。

Map.js: Map.js:

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

The file ../helpers/helper-country-point is : 文件../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
}

When I define pointToLayer inside Map.js and use: 当我在Map.js中定义pointToLayer并使用时:

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

It works. 有用。 Any idea of why I keep getting this error ? 知道为什么我会不断收到此错误吗?

In the first one, you are invoking the function and using this as the argument. 在第一个中,您要调用该函数并将this用作参数。

In the second one, you are not invoking the function. 在第二个中,您不调用该功能。 You are simply passing the function as props. 您只是将函数作为道具传递。 You can try removing the (this) part in your first example, so you only pass the function without invoking it. 您可以尝试在第一个示例中删除(this)部分,以便仅传递函数而不调用它。

Then, since the error is pointToLayer is not a function, and it is resolved when you declare the function in map.js , I suspect that you might have written the wrong path to the helper.js file. 然后,由于错误是pointToLayer而不是一个函数,并且在map.js声明该函数时已解决,因此我怀疑您可能将错误的路径写入了helper.js文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM