加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React:尝试将onClick添加到li标签但单击处理程序功

发布时间:2020-12-15 20:51:18 所属栏目:百科 来源:网络整理
导读:我是新手做出反应,所以我对此并不了解. 我正在尝试向li添加一个单击处理程序,但该函数似乎未定义? var ActivityList = React.createClass({ getInitialState: function() { return {name:"test"}; },handleClick:function(e){ console.log(e.target.name);
我是新手做出反应,所以我对此并不了解.
我正在尝试向li添加一个单击处理程序,但该函数似乎未定义?
var ActivityList = React.createClass({
  getInitialState: function() {
    return {name:"test"};
  },handleClick:function(e){
    console.log(e.target.name);
  },render:function(){
    return (
      <ul>
        {this.props.data.map(function(game){
         return <li onClick={this.handleClick} name={game.name}>{game.name}</li>;
        })
      }
      </ul>);
  }
});

我想知道是不是因为我在这里找错了?

将其设置为 .map,因为在 .map回调中这指的是全局范围(在浏览器中它是窗口或如果使用 strict mode则未定义)
this.props.data.map(function(game) {
  return <li onClick={this.handleClick} name={game.name}>{game.name}</li>;
},this);
  ^^^^^

Example

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读