【发布时间】:2018-12-17 09:50:39
【问题描述】:
我正在使用 react-google-maps 并且能够显示标记和 InfoWindow - 工作正常,但我只想显示当前活动的标记窗口。
我正在使用 isOpen 状态来存储当前标记并显示它是否与当前键匹配,但它仍然允许您单击并切换所有标记信息窗口。我怀疑我犯了一个非常简单的错误,但我对 ReactJS 还是很陌生。另外,请注意我发现了非常相似的问题,但没有一个可以帮助我解决这个问题。
这是我的代码..
constructor(props){
super(props);
this.state = {
isOpen: '',
}
}
handleToggleOpen = (e) => {
this.setState({
isOpen: e
});
console.log(e);
console.log('current state is... ');
console.log(this.state.isOpen);
}
handleToggleClose = () => {
this.setState({
isOpen: null
});
}
render() {
// console.log(this.props.marker.id)
return (
<div>
<Marker
key={this.props.marker.id}
position={{ lat: parseFloat(this.props.marker.acf.locations.lat), lng: parseFloat(this.props.marker.acf.locations.lng)}}
icon={{
url: this.props.icon,
optimized: false,
scaledSize: new google.maps.Size(25, 37),
}}
onClick={() => this.handleToggleOpen(this.props.marker.id)}
audioPlay={this.props.audioPlay} >
{
this.state.isOpen === this.props.marker.id && (
<InfoWindow onCloseClick={this.handleToggleClose}>
<div>
<h3 className="infowindow__title">{this.props.marker.title.rendered} {this.props.marker.acf.title_sub} </h3>
<span onClick={() =>this.refs.componentToDisplay.showPopup()} className="infowindow__link underline">Learn more</span>
</div>
</InfoWindow>)
}
<PlacePopup ref="componentToDisplay" title={this.props.marker.title.rendered} intro={this.props.marker.acf.text} id={this.props.marker.id} subtitle={this.props.marker.acf.title_sub} audio={this.props.marker.acf.file} audioPlay={this.props.audioPlay} >
</PlacePopup>
</Marker>
</div>
)
}
【问题讨论】:
标签: javascript reactjs react-google-maps