简体   繁体   English

将代码移动到函数中时,React-leaflet LayersControl 抛出错误

[英]React-leaflet LayersControl throws error when moving code into function

Sorry if it this has been answered somewhere else before!抱歉,如果之前在其他地方已经回答过这个问题! I am new to react-leaflet and struggle with this issue for some time now.我是 react-leaflet 的新手,并且在这个问题上挣扎了一段时间。

The following code does not compile and and the chrome developer tools tell me: 3 errors on this page:以下代码无法编译,并且 chrome 开发人员工具告诉我:此页面上有 3 个错误:
1) "TypeError: addOverlay is not a function", 1) "TypeError: addOverlay 不是函数",
2) "TypeError: addOverlay is not a function" and ", 2) "TypeError: addOverlay is not a function" 和 ",
3) "TypeError: this.props.removeLayer is not a function". 3)“类型错误:this.props.removeLayer 不是函数”。

What I do not understand is: if I comment out the "TestOverlay" component, it compiles.我不明白的是:如果我注释掉“TestOverlay”组件,它就会编译。 There seems to happen some magic with putting the code into a function, but I have no clue what is the problem..将代码放入函数中似乎发生了一些魔法,但我不知道问题出在哪里..

Using: "leaflet": "1.7.1", "react": "16.14.0", "react-dom": "16.14.0", "react-leaflet": "2.7.0",使用: "leaflet": "1.7.1", "react": "16.14.0", "react-dom": "16.14.0", "react-leaflet": "2.7.0",

Thanks a lot for the help!非常感谢您的帮助!

import React from "react";
import { Map, TileLayer, LayersControl, Marker, LayerGroup } from "react-leaflet";

import "./App.css";

function TestOverlay() {
  return <LayersControl.Overlay checked name="Random layer 2">
  <Marker position={[52.339545, 4.900526]} />
</LayersControl.Overlay>;

}

export default function App() {
  return (
    <Map center={[52.339545, 4.900526]} zoom={14}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
      <LayersControl position="topright">
        <LayersControl.Overlay checked name="Random layer">
          <Marker position={[52.339545, 4.900526]} />
        </LayersControl.Overlay>
        <TestOverlay/>
      </LayersControl>
    </Map>);
}

From the documentation1 , documentation2来自文档 1文档 2

Map: top-level component instantiating a Leaflet map and providing it to its children. Map:顶级组件实例化一个 Leaflet 地图并将其提供给它的孩子。

All components are React wrappers for Leaflet elements and layers, they need a map instance and therefore must be included in a top-level component.所有组件都是 Leaflet 元素和图层的 React 包装器,它们需要一个地图实例,因此必须包含在顶级组件中。

LayersControl.Overlay uses Overlay class( doc ) and inside Overlay class there is the following code: LayersControl.Overlay 使用 Overlay 类( doc )并且在 Overlay 类中有以下代码:

 class Overlay extends ControlledLayer {
  constructor(props: ControlledLayerProps) {
    super(props)
    this.contextValue = {
      ...props.leaflet,
      layerContainer: {
        addLayer: this.addLayer.bind(this),
        removeLayer: this.removeLayer.bind(this),
      },
    }
  }

  addLayer = (layer: Layer) => {
    this.layer = layer // Keep layer reference to handle dynamic changes of props
    const { addOverlay, checked, name } = this.props
    addOverlay(layer, name, checked)
  }
}

in the constructor addLayer is assigned a method which is this.addLayer .在构造函数addLayer中分配了一个方法,即this.addLayer inside this.addLayer addOverlay is being destructured via props.this.addLayer addOverlay 内部正在通过 props 进行解构。 Most likely at that point props do not contain addOverlay method and therefore cannot be retrieved so the error occurs.最有可能在那时 props 不包含 addOverlay 方法,因此无法检索,因此发生错误。

As a result you cannot use LayersControl.Overlay the way you are trying to.因此,您无法按照您尝试的方式使用LayersControl.Overlay There is not such an example and I think it is not possible as the map instance is not provided the way it should to LayersControl.Overlay没有这样的例子,我认为这是不可能的,因为地图实例没有按照应有的方式提供给LayersControl.Overlay

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

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