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