简体   繁体   English

反应和传单

[英]React and Leaflet

I'm trying to use Leaflet in my React App.我正在尝试在我的 React 应用程序中使用 Leaflet。 I'm running into an issue.我遇到了一个问题。 Leaflet.js requires the div component to pre-exist when initiating the map. Leaflet.js 要求 div 组件在启动地图时预先存在。 React doesn't "create" the div until it renders the component, so leaflet is throwing an error. React 在呈现组件之前不会“创建” div,因此传单会引发错误。 Both getDOMNode() and findDOMNode() return "not a function" for whatever reason.无论出于何种原因,getDOMNode() 和 findDOMNode() 都返回“非函数”。

Code:代码:

import React from 'react';
import {render} from 'react-dom';
import L from 'leaflet';

...a little later ……稍后

export default class Mapbox extends React.Component {
  render() {
    var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

    return (

      <div id="map">
    <h1>hi</h1>
    </div>
    );

This returns an error that "Map Container not Found".这将返回“未找到地图容器”的错误。

Thanks.谢谢。

You can initialize map inside componentDidMount您可以在componentDidMount内初始化地图

class Mapbox extends React.Component {
  componentDidMount() {
    this.map();
  }

  map() {
    var map = L.map('map').setView([51.505, -0.09], 13);

    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
  }

  render() {
    return <div id="map">xx</div>  
  }
}

Example

The other answers are great if you're using class components.如果您使用的是类组件,其他答案也很棒。 If you have to use it with functional components (using Hooks ) than you might need to code useRef .如果您必须将它与功能组件(使用Hooks )一起使用,那么您可能需要编写useRef

function Map({}) {
  // define the ref here
  const mapRef = useRef(null);

  useEffect( () => {
// set the initialized map to the ref
    mapRef.current = L.map('map').setView([51.505, 3], 13);
    
    }, []);

// pass it in the required div node
  return (
    <div ref={mapRef} id="map" className="p-2">
    </div>
  );

}

In this way the map will be initialized after the DOM node is rendered.通过这种方式,地图将在 DOM 节点渲染后进行初始化。

Reference: React hooks .参考:反应钩子

Since react 16.3 there is a new method for creating references easily.从 react 16.3有一种新方法可以轻松创建引用。

note:the references can be stored in constructor as the refernces can be created prior to the jsx div is created.注意:引用可以存储在构造函数中,因为可以在创建 jsx div 之前创建引用。

class Map extends React.Component {


  componentDidMount() {
   this.map = React.createRef();
   var map = L.map(this.map).setView([51.505, -0.09], 13);
  }

  render() {
    return <div ref={this.map}></div>
  }
}

Use "refs".使用“参考”。 Refs is used to return a reference to the element. Refs 用于返回对元素的引用。 docs here文档在这里

class Map extends React.Component {

  componentDidMount() {
    const node = this.node;
    var map = var map = L.map(node).setView([51.505, -0.09], 13);
  }

  render() {
    return <div ref={(node) => { this.node = node }}></div>
  }
}

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

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