react中使用map时onClick事件失效

it2022-06-22  78

分享一些踩过的坑

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

相关资源:数据结构—成绩单生成器

最新回复(0)