Closed johnnylc closed 4 years ago
Maybe u should insert ReactComponent to icon props? Like:
class ReactComponent extends Component {
render() {
const markerStyle = {
backgroundColor: "blue",
color: "white",
display: "flex",
justifyContent: "center",
width: "50px",
height: "50px",
borderRadius: "50px",
alignItems: "center"
};
return <div onClick={() => alert('rc click')} style={markerStyle}>Marker;
}
}
<Marker icon={ <ReactComponent /> } position={this.state.center} />
@johnnylc, @makhataibar has suggested the right way to use it.
Maybe u should insert ReactComponent to icon props? Like:
class ReactComponent extends Component { render() { const markerStyle = { backgroundColor: "blue", color: "white", display: "flex", justifyContent: "center", width: "50px", height: "50px", borderRadius: "50px", alignItems: "center" }; return <div onClick={() => alert('rc click')} style={markerStyle}>Marker; } } <Marker icon={ <ReactComponent /> } position={this.state.center} />
I tried this way but didn't work.
But we could use eventHandlers
prop from the react-leaflet
documentation here https://react-leaflet.js.org/docs/api-components/.
It worked for me.
<Marker
position={[50.5, 30.5]}
eventHandlers={{
click: () => {
console.log('marker clicked')
},
}}
/>
I posted a way to do this over on this SO answer and have published it as a lib on https://github.com/adamscybot/react-leaflet-component-marker
class ReactComponent extends Component { render() { const markerStyle = { backgroundColor: "blue", color: "white", display: "flex", justifyContent: "center", width: "50px", height: "50px", borderRadius: "50px", alignItems: "center" }; return <div onClick={() => alert('rc click')} style={markerStyle}>Marker
<Marker icon={ } position={this.state.center} />
When render the ReactComponent as marker, onClick event in div won't triger.