alexa

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); 
}

191 0
7

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

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry