我尝试创建一个对onClick事件做出反应的函数,但是该函数从未执行过,但是onClick工作正常 这是我的代码:
onEachFeature(feature, layer) {
console.log("In onEachFeature")
layer.on({
onClick: this.clickToFeature.bind(this)
});
}
clickToFeature(e) {
console.log("in click")
var layer = e.target;
console.log("I clicked on " + layer.feature.properties.name);
}
render() {
let markers = []
let nameAccomodation = null
// Add markers only when we received stays data
if (this.state.dataReceived) {
markers = this.state.stays.map((el) => {
return(
<Marker
position={[el.data.lat, el.data.lon]}
onpopupopen={(ev) => this.stayFocus(ev, el.id)}
onpopupclose={(ev) => this.stayUnfocus(ev, el.id)}>
<Popup>
{el.data.name}
</Popup>
</Marker>
)
});
nameAccomodation = this.state.stays.map((el) => {
return(
nameAccomodation=el.data.name
)
});
}
如果我用console.log(“”)替换“ this.clickToFeature.bind(this)”,则控制台将打印日志。 您知道如何解决我的问题吗? 谢谢!