admin管理员组文章数量:1410717
I need to pass the index of the menu item selected onChange but don't know how to access it.
const handleListChange = (e) => {
console.log('Item Index: ', e.target.key);
}
<TextField
select
label="Select item"
value={show}
onFocus={getListArray}
onChange={e => handleListChange(e)}
>
{listArray.map((value, index) =>
<MenuItem
key={index}
value={value.title}
>
{value.title}
</MenuItem>
)}
</TextField>
I need to pass the index of the menu item selected onChange but don't know how to access it.
const handleListChange = (e) => {
console.log('Item Index: ', e.target.key);
}
<TextField
select
label="Select item"
value={show}
onFocus={getListArray}
onChange={e => handleListChange(e)}
>
{listArray.map((value, index) =>
<MenuItem
key={index}
value={value.title}
>
{value.title}
</MenuItem>
)}
</TextField>
Share
Improve this question
asked Jul 16, 2021 at 0:53
sbadensbaden
5653 gold badges16 silver badges31 bronze badges
1 Answer
Reset to default 7You can use map in order to get an array of titles and indexOf to get the index of the selected item.
And here it is with ES6 and arrow syntax, which is even simpler:
const handleListChange = (e) => {
const index = listArray.map(item => item.title).indexOf(e.target.value);
console.log(index);
}
版权声明:本文标题:javascript - How do I access the index of a selected item in list using React and Material UI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744825209a2627019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论