简体   繁体   English

最新版本的 ReactJS 和 LeafLet.Shapefile 不工作,只显示空白页

[英]Lastest Version of ReactJS and LeafLet.Shapefile Not Work, only display blank page

I'm currently working on ReactJS GIS and referring to this sample Demo .我目前正在研究 ReactJS GIS 并参考此示例Demo But when I update the latest version of React and dependencies is not work, no error message displayed on the console.但是当我更新最新版本的 React 并且依赖项不起作用时,控制台上没有显示错误消息。 What wrong.有什么不对。 I've also test in my local computer, the result is same (not work) what happen?我也在本地计算机上进行了测试,结果相同(不起作用)会发生什么?

The demo:演示: 在此处输入图像描述

After update the dependencies:更新依赖项后: 在此处输入图像描述

There are many things that have been changed:有很多东西已经改变:

  1. React has changed to 18.x. React 已更改为 18.x。 You should replace in index.js all the code您应该在 index.js 中替换所有代码

    import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <App /> );
  2. react-leaflet has moved to version 4.x which has breaking changes: react-leaflet 已移至具有重大更改的 4.x 版本:

map ref is received via a ref and a useState hook and no need for map.current or map.current.leafletElement . map ref 是通过 ref 和 useState 钩子接收的,不需要map.currentmap.current.leafletElement Also Map comp has been renamed to MapContainer Map comp 也已重命名为MapContainer

 const [map, setMap] = useState(null);

  useEffect(() => {
    if (!map) return;

    map.setView([34.74161249883172, 18.6328125], 2);
  }, [map]);

<MapContainer
      center={position}
      zoom={13}
      style={{ height: "100vh" }}
      ref={setMap}
    >
  1. mapRef in MapContainer's child components is received via MapContainer 的子组件中的 mapRef 是通过

    const map = useMap();

Demo 演示

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

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