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相当于箭头函数


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM