Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

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

By M.K. Verma · 2 May 2022

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