使用react-leaflet渲染GeoJSON

20

我有一个简单的组件,可以在地图上选择点,然后显示与该点相关的一些GeoJSON数据:

import React, { Component } from 'react';

import { Map, Marker, TileLayer, GeoJSON } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

import { connect } from 'react-redux';
import './style.css';

import { setPoint, fetchData } from '../../actions/map';

@connect(store => {
  return {
    map: store.map
  }
})
class MyMap extends Component {

  mapClick(e) {
    this.props.dispatch(setPoint(e.latlng));
    this.props.dispatch(fetchData(e.latlng));
  }

  render() {
    const marker = () => {
      if(this.props.map.point) {
        return <Marker position={this.props.map.point} />;
      }
    };

    const data = () => {
        if(this.props.map.data.length > 0) {
            const json = this.props.map.data;
            return <GeoJSON data={json} />
        }
    }

    return (
      <div className='my-map'>
        <div className='my-map__map-container'>
          <Map center={this.props.map.center} zoom={13} onClick={this.mapClick.bind(this)}>
            <TileLayer url='http://{s}.tile.osm.org/{z}/{x}/{y}.png' attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' />
            {marker()}
            {data()}
          </Map>
        </div>
        <div className='my-map__debug'>
            {JSON.stringify(this.props.map)}
        </div>
      </div>
    );
  }
}

export default MyMap;
第一次单击地图标记后,经过一段时间(XHR请求),GeoJSON渲染。但是下一次我点击地图时,只有标记位置发生了变化,旧的GeoJSON数据仍然在地图上。组件的调试部分可以正常呈现,并显示正确的数据。我如何强制react-leaflet重新渲染我的GeoJSON数据,或者我可能做错了什么?

更新:询问react-leaflet作者后,我找到了实现期望行为的方法。要强制React重新渲染我的GeoJSON数据,我需要向组件传递一些唯一的数据键。
<GeoJSON key={keyFunction(this.props.map.data.json)} data={this.props.map.data.json} />

https://github.com/PaulLeCam/react-leaflet/issues/332#issuecomment-304228071

3个回答

24

1
你有没有可能编辑原始帖子,将你找到的完整最终解决方案添加进去? - DGaffneyDC
17
你使用了什么关键函数? - 39fredy

7
import hash from 'object-hash';

render() {
let blah = this.state; //or this.props or whatever
<GeoJSON
  key={hash(blah)}
  data={blah} />

1

每次调用渲染方法时都创建一个函数来调用标记和geojson不会很高效。此外,我认为您可能需要为地图元素添加键,以便它们可以得到正确的重用和更新。

试一试这个:

render() {

    return (
      <div className='my-map'>
        <div className='my-map__map-container'>
          <Map center={this.props.map.center} zoom={13} onClick={this.mapClick.bind(this)}>
            <TileLayer url='http://{s}.tile.osm.org/{z}/{x}/{y}.png' attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' />
            {this.props.map.point &&
              <Marker key='my-marker' position={this.props.map.point} />
            }
            {this.props.map.data.length > 0 && 
              <GeoJSON key='my-geojson' data={this.props.map.data.json} />
            }
          </Map>
        </div>
        <div className='my-map__debug'>
            {JSON.stringify(this.props.map)}
        </div>
      </div>
    );
  }

我按照你的建议重写了这段代码,但问题仍然存在 - JSON 数据只渲染一次 :( - Titenko Stanislav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接