React-map-gl: Popup won't remove

Created on 26 Jul 2018  路  2Comments  路  Source: visgl/react-map-gl

I successfully added a popup on my map, but I can't figure out how to close the popup. (I think it's a bug?)

I imported the Popup;

import MapGL, { Popup } from 'react-map-gl';

and defined the popup method like the following;

_renderPopup() {
  return(
    <Popup
      anchor="bottom"
      tipSize={10}
      longitude={this.state.popupInfo.lon}
      latitude={this.state.popupInfo.lat}
      closeButton={true}
      closeOnClick={true}
      >
        <div style={style_popup}>
          <p> lon = {this.state.popupInfo.lon} </p>
          <p> lat = {this.state.popupInfo.lat} </p>
        </div>
    </Popup>
  );
}

which I rendered like this;

render() {
  const {viewport, data, popupInfo} = this.state;
    return (
      <MapGL
        {...viewport}
        {...this.props}
        onViewportChange={this._onViewportChange.bind(this)}
        mapboxApiAccessToken={MAPBOX_TOKEN}
        mapStyle={MAP_STYLE}
      >
        {this._renderPopup()}
      </MapGL>
    );
  }
}

It renders properly and the close button is on the popup. It just won't close. Any ideas? :/

Most helpful comment

@soap-tastes-ok

Just do this:

<MapGL
  onClick={() => this.setState({ popupInfo: null })}
  className={classes.map}
  mapStyle={mapStyle}
  {...this.state.viewport}
  onViewportChange={this._onViewportChange}
  mapboxApiAccessToken={MAPBOX_KEY}
>
  // your code
</MapGL>

All 2 comments

@soap-tastes-ok All react-map-gl components are stateless. You still need to subscribe to the onClose event and rerender without the popup. See the source code of this example.

@soap-tastes-ok

Just do this:

<MapGL
  onClick={() => this.setState({ popupInfo: null })}
  className={classes.map}
  mapStyle={mapStyle}
  {...this.state.viewport}
  onViewportChange={this._onViewportChange}
  mapboxApiAccessToken={MAPBOX_KEY}
>
  // your code
</MapGL>

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liang3404814 picture liang3404814  路  3Comments

miccferr picture miccferr  路  4Comments

nip10 picture nip10  路  4Comments

tbergman picture tbergman  路  3Comments

SethHamilton picture SethHamilton  路  3Comments