Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

How to get the index of the selected item from a dropdown in ReactJs and material UI ?

How to get the index of the selected item from a dropdown in ReactJs and material UI ?

You are mapping over the elements, the map method gives you the indices of the element, you could bubble the index of the given selected element via handler using the onClick prop

 {props.dropDownItems.map((element, index) => (
<MenuItem key={element + index} value={element} onClick={()=> handleItemClick(index)} > 
{element}
</MenuItem> 
))} 

The handleItemClick code will be as in the following

 const handleItemClick = (idx) => {
console.log(idx); 
}

7 0

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online