Search code examples
react-leaflet

Popup always open in the marker


Is there any way the popup always stays open and does not need to click on it to open?


Solution

  • What you can do is to make your own Marker class from the react-leaflet marker, and then call the leaflet function openPopup() on the leaflet object after it has been mounted.

    // Create your own class, extending from the Marker class.
    class ExtendedMarker extends Marker {
        componentDidMount() {
            // Call the Marker class componentDidMount (to make sure everything behaves as normal)
            super.componentDidMount();
    
           // Access the marker element and open the popup.
          this.leafletElement.openPopup();
        }
    }
    

    This will make the popup open once the component has been mounted, and will also behave like a normal popup afterwards, ie. on close/open.

    I threw together this fiddle that shows the same code together with the basic example.