React-mapbox-gl: How to close a popup

Created on 27 Feb 2017  路  9Comments  路  Source: alex3165/react-mapbox-gl

Is there any documentation about how to close a popup element?

Most helpful comment

@MikeKerrBBC something like this https://www.webpackbin.com/bins/-KqwVeENaV7MY4Xy4_vB

All 9 comments

You just need to unmount the Popup component, it behave like a normal react component

I have something like this

<Map
          className={styles.mapbox}
          center={[distributionLng, distributionLat]}
          style="mapbox://styles/mapbox/light-v9"
        >
          <Layer
            type="symbol"
            id="marker"
            layout={{ "icon-image": "harbor-15" }}
          >
            <Feature
              coordinates={[-87.622554, 41.882534]}
              onClick={() => {
                showPopup = true;
                console.log("on feature click", showPopup);
              }}
            />
          </Layer>
          <ZoomControl position="bottomLeft" />

          {showPopup &&
            <Popup
              coordinates={[-87.622554, 41.882534]}
              offset={{
                "bottom-left": [12, -38],
                bottom: [0, -38],
                "bottom-right": [-12, -38]
              }}
              anchor="top-left"
              onClick={() => {
                showPopup = false;
                console.log("on Popup click", showPopup);
              }}
            >
              <h1>Popup</h1>
            </Popup>}
       </Map>

I want to be able to toggle the tooltip on each feature.
I feel there should be more documentation on this process...

@alex3165 Do I need to trigger a re-render of the map? I can see my boolean showPopup going from false to true but the Popup never renders.

You just need to put your boolean into the state of your component, with setState, like you would do with anything else in react, nothing specific to react-mapbox-gl here.

Well, @Wykks this might become unmanageable in the future with a lot of points. Let me stepback... what is the recommended way to make Popups for each of your feature the open up onClick?

in the original MapBox documentation you do something like this inClick

  <Feature
              coordinates={[-87.622554, 41.882534]}
              onClick={() => {
             new Popup()
                  .setLngLat([-87.622554, 41.882534])
                  .setHTML(
                    `<h1>Popup</h1>
                              Hello Friend
                              </Popup>`
                  )
                  .addTo(Map);
              }}
            />

What is the react-mapbox-gl way?

I want to do this basically
https://www.mapbox.com/mapbox-gl-js/example/popup-on-click/

@MikeKerrBBC something like this https://www.webpackbin.com/bins/-KqwVeENaV7MY4Xy4_vB

Yes, thank you!

@Wykks @MikeKerrBBC the link is broken :( can you please share the idea again?

@tamycova You can access the solution here: https://webpackbin-prod.firebaseapp.com/bins/-KqwVeENaV7MY4Xy4_vB

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DaveBathnes picture DaveBathnes  路  3Comments

rofrol picture rofrol  路  4Comments

kolharsam picture kolharsam  路  4Comments

jonheslop picture jonheslop  路  4Comments

Ricardo-C-Oliveira picture Ricardo-C-Oliveira  路  4Comments