Basically, I want to pass the value associated with span element to the function UpdateSelectedNumbers(). However, if I create an array of span elements using for loop the onClick eventhandler ( UpdateSelectedNumbers) is always called with value 9. This is not the case when i use the map function. Can someone explain why this happens
const stars = [1,2,3,4,5,6,7,8,9];
var temp = [];
for(var i=0;i<9;i++){
temp.push(<span className={getClass(i+1)} onClick={()=>props.UpdateSelectedNumbers(i+1)}>{i+1} </span>)
}
return (
<div>
{stars.map(x => <span className={getClass(x)} onClick={()=> props.UpdateSelectedNumbers(x)}>{x}</span>)}
{temp}
</div>
)