ishaan6395 / react-leaflet-enhanced-marker

This library will help render dynamic react components as markers on leaflet maps easily
MIT License
39 stars 10 forks source link

cannot triger click event in react component #4

Closed johnnylc closed 4 years ago

johnnylc commented 5 years ago

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.

makhataibar commented 5 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} />
ishaan6395 commented 4 years ago

@johnnylc, @makhataibar has suggested the right way to use it.

jaballogian commented 2 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} />

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')
    },
  }}
/>
adamscybot commented 10 months ago

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