Javascript 挂接eventhandler时REACT,js中出现UnCaughtType错误

Javascript 挂接eventhandler时REACT,js中出现UnCaughtType错误,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,我正在添加下拉列表,并尝试将事件处理程序添加到每个项目中,并在按钮中显示所选项目。当我尝试在REACT.js中的菜单项上单击事件挂钩时,我收到此错误 未捕获的TypeError:无法读取未定义的属性“subscribeToEvents”。下面是我的班级 导出默认类位置列表扩展React.Component{ 建造师(道具) { 超级(道具); this.state={selectedIndex:“Location”, 数据:{ 城市:[“地点”、“班加罗尔”、“钦奈”、“浦那”] } };

我正在添加下拉列表,并尝试将事件处理程序添加到每个项目中,并在按钮中显示所选项目。当我尝试在REACT.js中的菜单项上单击事件挂钩时,我收到此错误

未捕获的TypeError:无法读取未定义的属性“subscribeToEvents”。下面是我的班级

导出默认类位置列表扩展React.Component{
建造师(道具)
{
超级(道具);
this.state={selectedIndex:“Location”,
数据:{
城市:[“地点”、“班加罗尔”、“钦奈”、“浦那”]
} 
};
}
getInitialState()
{
}
订阅事件(事件)
{
}
render()
{
var titleVal=this.state.selectedIndex;
控制台日志(标题);
返回(
{titleVal}
    {this.state.data.cities.map(函数(城市)){ 如果(城市!=“位置”) { return
  • } }) }
); }
}
您的问题是,
在传递给
.map()
的函数中未定义

解决方案1:绑定此文件的正确上下文

this.state.data.cities.map(function(city) {
    if(city !== 'Location') {
        return <li onClick={this.subscribeToEvents.bind(this)} ><a href="#" >{city}</a></li>
    }
}.bind(this));

解决方案1不起作用,解决方案2起作用。谢谢你的解决方案。
this.state.data.cities.map(city => {
    if(city !== 'Location') {
        return <li onClick={this.subscribeToEvents.bind(this)} ><a href="#" >{city}</a></li>
    }
});