0

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?

Hugo S. Mendes
  • 1,076
  • 11
  • 23
bsky
  • 19,326
  • 49
  • 155
  • 270

0 Answers0