The following code is part of a dropdown with all the months of the year, where Constants.MONTHS
is a list of all months.
var months = Constants.MONTHS;
for(var i = 0 ; i < months.length ; i++) {
items.push(<li onClick={()=>this.chooseMonth(i)}><a href="#">{months[i]}</a></li>);
}
return items;
My issues is that whichever item in the dropdown list is clicked, the method this.chooseMonth
is called with parameter 12
.
So it looks like this.chooseMonth(i)
only gets the value of i
from the last iteration.
How can I make every item in the dropdown call this.chooseMonth
with a distinct index?