首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应传单标记器不起作用

反应传单标记器不起作用
EN

Stack Overflow用户
提问于 2022-04-29 19:42:15
回答 2查看 1.2K关注 0票数 4

我正在尝试使用react-leaflet-markercluster,但在向代码中添加<MarkerClusterGroup>之后,控制台中会显示错误,页面不会加载。如果不添加此组件,一切都将正常工作。

也许问题在于版本冲突,但是我试图回滚react-leaflet的版本,但是在安装react-leaflet@3.2.1时也会发生冲突。因此,我决定集中精力处理实际版本。

App.js

代码语言:javascript
复制
import { MapContainer, TileLayer, Marker } from 'react-leaflet'
import MarkerClusterGroup from 'react-leaflet-markercluster'
import 'leaflet/dist/leaflet.css'
import 'react-leaflet-markercluster/dist/styles.min.css'

const App = () => {
  return (
    <div className="container">
      <MapContainer className="map-container" center={[49.8397, 24.0297]} zoom={6}>
        <TileLayer
          attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />        
        <MarkerClusterGroup>
          <Marker position={[49.8397, 24.0297]} />
          <Marker position={[52.2297, 21.0122]} />
          <Marker position={[51.5074, -0.0901]} />
        </MarkerClusterGroup>
      </MapContainer>
    </div>
  )
}

export default App

错误:

代码语言:javascript
复制
Uncaught Error: No context provided: useLeafletContext() can only be used in a descendant of <MapContainer>
    at useLeafletContext (context.js:9:1)
    at usePath (path.js:18:1)
    at ContainerComponent (component.js:6:1)
    at renderWithHooks (react-dom.development.js:16141:1)
    at updateForwardRef (react-dom.development.js:19968:1)
    at beginWork (react-dom.development.js:22391:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4157:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4206:1)
    at invokeGuardedCallback (react-dom.development.js:4270:1)
    at beginWork$1 (react-dom.development.js:27243:1)

package.json依赖关系:

代码语言:javascript
复制
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.1",
"leaflet": "^1.8.0",
"leaflet.markercluster": "^1.5.3",
"react-leaflet": "^4.0.0",
"react-leaflet-markercluster": "^3.0.0-rc1"
EN

回答 2

Stack Overflow用户

发布于 2022-06-27 19:19:07

@akhtem也许可以试试这个?https://www.npmjs.com/package/@changey/react-leaflet-markercluster。此链接解决了react 18和传单v4兼容性问题。我让它开始工作了。希望能帮上忙,谢谢

票数 2
EN

Stack Overflow用户

发布于 2022-04-29 20:01:42

您的ContainerComponent组件使用"useLeafletContext“,它只能在MapContainer ContainerComponent MapContainer等子组件中使用。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72062984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档