为什么组件不显示反应?

时间:2017-01-26 08:15:12

标签: reactjs leaflet react-router react-dom react-leaflet

我正在尝试使用leaflet做出反应..我参考了这个模块 https://github.com/PaulLeCam/react-leaflet https://www.npmjs.com/package/react-leaflet 但它没有显示地图为什么?

这是我的代码 https://plnkr.co/edit/pTpPxhEFqouMrLTrKUFq?p=preview

//代码在这里

const { Map, TileLayer, Marker, Popup } = ReactLeaflet;
class A extends React.Component{
  constructor() {
    super();
    this.state = {
      lat: 51.505,
      lng: -0.09,
      zoom: 13,
    };
  }
  render(){
    return (
    const position = [this.state.lat, this.state.lng];
    return (

      <Map center={position} zoom={this.state.zoom}>
      <label>cccc</label>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
        <Marker position={position}>
          <Popup>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </Marker>
      </Map>
    );
      )
  }
}

ReactDOM.render(<A/>,document.getElementById('example'));

1 个答案:

答案 0 :(得分:1)

render中有两个返回语句。

render应该只返回一次组件。

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

      <Map center={position} zoom={this.state.zoom}>
      <label>cccc</label>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
        <Marker position={position}>
          <Popup>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </Marker>
      </Map>
    );
  }

多件物品:

{this.state.items.map(item => (
    <Marker position={[item.lat, item.lng]}>
        <Popup>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
        </Popup>
     </Marker

 )}