【问题标题】:Re-rendering of react leaflet map is happening正在重新渲染反应传单地图
【发布时间】:2021-01-27 20:40:39
【问题描述】:

我是 React 和开发 covid-19 跟踪器应用程序的新手。我有一个国家/地区下拉列表,当我们从下拉列表中选择任何国家/地区时,将进行 api 调用,并使用反应挂钩(useState)显示电晕病例、死亡病例和康复病例。使用 onCountryChange 函数会发生这种情况;

const onCountryChange = async (event) => {
    const countryCode = event.target.value;
    setCountry(countryCode);

    const url =
      countryCode === 'worldwide'
        ? 'https://disease.sh/v3/covid-19/all'
        : `https://disease.sh/v3/covid-19/countries/${countryCode}`;
    await fetch(url)
      .then((response) => response.json())
      .then((data) => {
        setCountry(countryCode);
        setCountryInfo(data);

        setMapCenter([data.countryInfo.lat, data.countryInfo.long]);
        setMapZoom(4);
      });
  };

我已经引入了 react 传单地图并有一个状态

[ mapCenter, setMapCenter ] = useState({lat: 34.80746, lng: -40.4796})

现在在 onCountryChange 我正在设置 setMapCenter([data.countryInfo.lat, data.countryInfo.long]);但地图未以所选国家/地区为中心。任何人都可以帮助我这里有什么问题。 这是 github 仓库。

https://github.com/sohailshams/covid-19-tracker

【问题讨论】:

    标签: reactjs leaflet react-leaflet


    【解决方案1】:

    首先,您不会在每次发出请求时都更改纬度坐标。

     const onCountryChange = async (event) => {
        const countryCode = event.target.value;
        setCountry(countryCode);
    
        const url =
          countryCode === "worldwide"
            ? "https://disease.sh/v3/covid-19/all"
            : `https://disease.sh/v3/covid-19/countries/${countryCode}`;
        await fetch(url)
          .then((response) => response.json())
          .then((data) => {
            console.log(data);
            setCountryInfo(countryCode);
            setCountryInfo(data);
            const {
              countryInfo: { lat, long },
            } = data;
            setMapCenter({ lat, lng: long }); // here change the coordinates of the selected country
          });
      };
    

    其次,使用组件通过将新坐标作为道具传递来更改地图视图,因为更改中心是不够的,因为它在 3.x 版本上是不可变

    function ChangeMapView({ coords }) {
      const map = useMap();
      map.setView([coords.lat, coords.lng], map.getZoom());
    
      return null;
    }
    

    并在您的地图组件中使用它:

    function Map({ center, zoom }) {
      return (
        <div className='map'>
          <LeafletMap center={center} zoom={zoom}>
            <TileLayer
              url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            />
            <ChangeMapView coords={center} /> //here use it by passing the center coords
          </LeafletMap>
        </div>
      );
    }
    

    编辑

    这是demo

    我明确地使用了你的 github 代码来重现问题,后来 我看到您已经更改了一些代码。 通过使用这条线

    setMapCenter([data.countryInfo.lat, data.countryInfo.long]);
    

    您正在更改变量的类型,该变量最初是一个对象,然后突然变成了一个数组。在我看来,你不应该这样做。但最后,如果你想这样做,只需将 ChangeMapView comp 稍微更改为

    function ChangeMapView({ coords }) {
      const map = useMap();
      map.setView([coords[0], coords[1], map.getZoom());
    
      return null;
    }
    

    您的问题再次得到解决

    【讨论】:

    • 感谢您的回答,实际上,当我们更改国家/地区时,它确实改变了坐标。你可以看到 console.log(data.countryInfo.lat) console.log(data.countryInfo.long) 当我将值设置为 setMapCenter=([data.countryInfo.lat, data.countryInfo.long]);它应该在我通过中心时更改地图位置并作为道具缩放到地图组件。
    • 没关系,因为您将 center={mapCenter} 传递给 Map 组件,因此 mapCenter 变量具有您应该更改的坐标,以便在 Map 上接收它们并更改地图视图。在不同的场景中,您需要更改要传递给 Map 的变量。 setMapCenter 不在您的 github 代码中的任何地方使用
    • 非常感谢您的努力和帮助。但是,如果我只是在 Map.js 中添加以下代码,则地图会更新并定位到更新的纬度和经度。 function ChangeMap({ center, zoom }) { const map = useMap(); map.setView(中心,缩放);返回空值; }
    • 您不想根据所选国家的坐标来居中地图吗?那你想达到什么目标?
    • 是的,我想根据所选国家/地区将地图居中,此功能也可以。但是我喜欢你的方法,并且会同意。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2020-11-20
    • 2016-07-13
    • 2022-01-04
    • 2019-12-13
    • 2018-12-16
    相关资源
    最近更新 更多