I have a for loop in my render function, its supposed for render 5 stars from the fontawesome library, a rating of 3 should render 3 full stars and 2 empty stars....
the issue i am having is that my iterator "i" is returning as 5 for all of them. atleast in my onClick handler. But what is really weird is that if use the same iterator variable "i" as the id or any other custom prop for the element it displays the correct 1,2,3,4, or 5. Why does this happen?
import Component from "inferno-component";
import styles from "./styles";
export default class StarRating extends Component {
constructor() {
super();
this.state = {
rating: 3
};
}
rate = rating => {
console.log(rating);
this.setState({
rating: rating
});
};
render() {
var stars = [];
for (var i = 0; i < 5; i++) {
var klass = "fa fa-star";
if (this.state.rating <= i && this.state.rating != null) {
klass += "-o";
}
stars.push(
<i
style={styles.star}
className={klass}
id={i}
aria-hidden="true"
onClick={() => this.rate(i)}
/>
);
}
return <span>{stars}</span>;
}
}