简体   繁体   English

未捕获的错误:未提供上下文:useLeafletContext() 只能用于<mapcontainer></mapcontainer>

[英]Uncaught Error: No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>

I'm using react-leaflet and I added a marker with an event Handler on click to zoom to this marker, but when I try to use const map=useMap() all I get is this error =>:我正在使用 react-leaflet 并在单击时添加了一个带有事件处理程序的标记以缩放到该标记,但是当我尝试使用 const map=useMap() 时,我得到的只是这个错误 =>:

Uncaught Error: No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>

There might be similar questions but none have answers, can anyone please help with this?可能有类似的问题,但没有答案,有人可以帮忙吗? This is my code:这是我的代码:

const map = useMap()
 return (
        <div>
          <MapContainer
            className="leaflet-container"
            center={[33.8735578, 35.86379]}
            zoom={9}>

            <TileLayer
              attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />

            <Marker
              icon={port}
              eventHandlers={{
                click: (e) => {
                  map.setView(e.latlng, 14);
                },
              }}
              position={[33.90757548098519, 35.51700873340635]}
            ></Marker>
          </MapContainer>

Thanks!谢谢!

well I solved it by making a separate component for the marker and implement the useMap() in it and it worked as so:好吧,我通过为标记制作一个单独的组件并在其中实现 useMap() 来解决它,它是这样工作的:

import React from "react";
import { Marker, useMap } from "react-leaflet";

export default function Markerwhatever(props) {
  const map = useMap();

  return (
    <div>
      <Marker
        icon={props.icon}
        position={[33.91907336973602, 35.51552625946782]}
        eventHandlers={{
          click: (e) => {
            map.flyTo(e.latlng, 14);
          },
        }}
      ></Marker>
    </div>
  );
}

暂无
暂无

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

相关问题 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component Dropzone - 未捕获的错误:没有提供 URL - Dropzone - Uncaught Error: No URL provided Dropzone:未捕获的错误:未提供 URL - Dropzone : Uncaught Error: No URL provided React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component 未捕获的错误:提供的 DOM 元素是 null 或在 plotly JS 中未定义 - Uncaught Error: DOM element provided is null or undefined in plotly JS (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component RCTTextField不是RCTShadowView错误的后代 - RCTTextField is not a descendant of RCTShadowView error 我如何解析“ useHref() 只能在上下文中使用<router>组件”当组件已经在里面时<router> ?</router></router> - How can I resolver " useHref() may be used only in the context of a <Router> component" when component is already inside <Router>? 未捕获错误:定义不能间接使用 - Uncaught Error: define cannot be used indirect 未捕获(承诺)DOMException:无法启动视频源,未捕获错误:您提供的错误不包含堆栈跟踪 - Uncaught (in promise) DOMException: Could not start video source , Uncaught Error: The error you provided does not contain a stack trace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM