React-leaflet 如何获取地图边界和居中呢?

React-leaflet how to get map bounds and center it?

我想获取这张地图的边界并将其居中 map
我尝试使用 getBounds() 和 getCenter() 但未定义。
然后我找到了这个但是它说无法读取未定义的 属性 'leafletElement'
感谢您对转发的帮助。

class FortMap extends Component {
  state = {
    lat: 51.505,
    lng: -0.09,
    zoom: 18,
  }

  componentDidMount(){
    console.log(this.refs.map.leafletElement.getBounds);
  }

  
  render() {
    const { lat , lng , zoom } = this.state;
    const position = [0, 0];

    return (
        <Map center={position} zoom={zoom}>
          <TileLayer
            url={fortniteMap}
            attribution="&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
          />
        </Map>
    )
  }
}

export default FortMap;

您似乎忘记为 Map 组件分配 ref 属性:

<Map ref='map' center={position} zoom={zoom}>
  ...
</Map>

获取传单实例的引用:

componentDidMount(){
    let mapInst =  this.refs.map.leafletElement;
}

Demo