try:
heads.forEach((head)=>{
tHead.push(<th onClick={() => this.handleRowClick(head)> {head} </th>});
}
The advantage of using forEach over a for loop is that the index and value are bound to a specific loop iteration instead of the surrounding scope. Thus when the loop index changes the closures keep the old loop-index.
This will also work:
heads.forEach((_, index)=>{
tHead.push(<th onClick={() => this.handleRowClick(heads[index])> {heads[index]} </th>});
}
or this:
for (var i = 0; i < size;i++){
let j = i;
tHead.push(<th onClick={() => this.handleRowClick(heads[j])}> {heads[j]} </th>);
}
The last one works because j
is bound to a specific loop iteration, so it will not change while i
will.