gpt4 book ai didi

reactjs - 单击 ES6 React 获取关键索引

转载 作者:行者123 更新时间:2023-12-03 13:11:44 24 4
gpt4 key购买 nike

我有以下组件

const list = (props) => {

const handler = function(){

};

var listItems = props.items.map(function(item, index){
return (
<li key={index} onClick={ handler }>
{item.text}
</li>
)
});

return (
<div>
<ul>
{listItems}
</ul>
</div>
)
}

单击时我想获取单击的 li 的索引。使用 ES6 且无需绑定(bind)我该如何做到这一点?

最佳答案

使用箭头函数。

onClick={() => handler(index)}

关于reactjs - 单击 ES6 React 获取关键索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com