react中使用map时onClick事件失效

分享一些踩过的坑

react中使用map时onClick事件失效

    <span>
      {
        count.map(
          (item,index)=>{
            return <span style={{paddingRight:2,cursor:'pointer'}} key={index} onClick={handleEditCount(item[1],item[2])}>{item[0]}<Icon type="edit" /></span>
          }
        )
      }
    </span>

这样是真的失效吗

不,只是不会用而已

其实把函数改成箭头函数就能用啦

    <span>
      {
        count.map(
          (item,index)=>{
            return <span style={{paddingRight:2,cursor:'pointer'}} key={index} onClick={() => handleEditCount(item[1],item[2])}>{item[0]}<Icon type="edit" /></span>
          }
        )
      }
    </span>

顺便说一下 .bind相当于箭头函数

原文地址:https://www.cnblogs.com/piaobodewu/p/10232685.html