It can be done without useMemo and useRef however still using eventHandlers:
<Marker
position={[latitude, longitude]}
icon={getIcon(markerType)}
eventHandlers={{
mouseover: (event) => event.target.openPopup(),
}}
>
<Popup>Hello</Popup>
</Marker>;
Answer from Disco on Stack OverflowIt can be done without useMemo and useRef however still using eventHandlers:
<Marker
position={[latitude, longitude]}
icon={getIcon(markerType)}
eventHandlers={{
mouseover: (event) => event.target.openPopup(),
}}
>
<Popup>Hello</Popup>
</Marker>;
You need to create a custom component and then one way would be to use eventHandlers to handle mouseover and mouseout events in combination with the marker ref to call leaflet's native openPopup and closePopup methods respectively.
const RenderIcons = () => {
const markerRef = useRef();
const eventHandlers = useMemo(
() => ({
mouseover() {
if (markerRef) markerRef.current.openPopup();
},
mouseout() {
if (markerRef) markerRef.current.closePopup();
}
}),
[]
);
return (
<Marker
ref={markerRef}
position={position}
icon={icon}
eventHandlers={eventHandlers}
>
<Popup>Hello</Popup>
</Marker>
);
};
Demo
How to toggle popup in react-leaflet on mouse hover
tooltip div with ReactJS - javascript
Popup: can be triggered on hover and click
How To Keep An Inner Menu Open As You Hover Over The Button?
Videos
ยป npm install react-modal-hover
Leaflet Marker object is accessible via event.target property of mouseover and mouseout events, so popup could be opened/closed like this:
<Marker
position={position}
onMouseOver={(e) => {
e.target.openPopup();
}}
onMouseOut={(e) => {
e.target.closePopup();
}}
>
<Popup>Sydney</Popup>
</Marker>
Demo
Using React Leaflet v3, the solution is easier and cleaner, use Tooltip instead of Popup, e.g.:
<Marker position={this.props.position}>
<Tooltip>I appear on mouse over</Tooltip>
</Marker>
In particular, I have found useful to also add sticky property for Tooltip. There are more examples for tooltips in the documentation, which covers basic tooltips, sticky tooltips or permanent tooltips, with offsets and many more.
You can make your component to return the following markup
return (
<div>
<div onMouseOver={this.handleMouseIn.bind(this)} onMouseOut={this.handleMouseOut.bind(this)}>on hover here we will show the tooltip</div>
<div>
<div style={tooltipStyle}>this is the tooltip!!</div>
</div>
</div>
);
Where tooltipStyle is assigned like this:
const tooltipStyle = {
display: this.state.hover ? 'block' : 'none'
}
So tooltip depends on component state, now in handleMouseIn and handleMouseOut you need to change component state to make tooltip visible.
handleMouseIn() {
this.setState({ hover: true })
}
handleMouseOut() {
this.setState({ hover: false })
}
Here is working example.
You can start diving in React with this article: Thinking in React.
I think whatever you want to show as tooltip, just add that to the "title" of the div where you want to show it.
Eg:
<div title="I am the tooltip text">I am the div where you should hover</div>
But if its a custom designed div then go as the answers given before.