首页 > 解决方案 > 谷歌地图 - 使用 React 使列表可点击

问题描述

我需要使侧边栏列表可单击,因此当您单击侧边栏上的某个位置时,其相应的标记将打开信息窗口。下面是我使用的代码:

点击列表功能:

clickListItem = venues => {
    const marker = this.state.markers.find(marker.id === venues.id);
    this.handleMarkerClick(marker);
    console.log(venues.name);
  };

然后我尝试在侧边栏调用它:

<List>
            {this.props.venues &&
              this.props.venues.map(({ venue }, index) => (
                <ListItem
                  button
                  key={index}
                  onClick={() => this.props.clickListItem(this.props)}
                >
                  <ListItemText primary={venue.name} />
                </ListItem>
              ))}
          </List>

当我点击一个位置时,什么也没有发生。如何让列表调用打开信息窗口的标记函数?完整代码在这里

标签: reactjslistgoogle-maps-api-3google-maps-markersclickable

解决方案


Sidebar.js,第 154 行。首先,尝试将其更改为:

onClick={() => this.props.clickListItem(venue)}


推荐阅读