I have a button, that is part of an array in jsx.
The button displays the index, 1, 2, 3, etc. When I press the button however and console log it I get the same value each time, the length of the for loop. I cannot wrap my head around it. I was thinking of maybe having the wrong keys and coupling two components, but that's not the problem.
const getRows = () => {
var rows = [];
for (var i = 0; i < Math.max(dropped2 && dropped2.length, dropped1 && dropped1.length); i++) {
console.log(i);
rows.push(
<div key={i}>
<Browser>
<DropItArranged left dropped={dropped2 && dropped2.length > i && dropped2[i]} index={i} selected={displayIndex === i} />
<DropItArranged dropped={dropped1 && dropped1.length > i && dropped1[i]} index={i} selected={displayIndex === i} />
<button key={"selectButton" + i} className={displayIndex === i ? "button is-link" : "button"} onClick={e => { console.log(i); dispatch(allActions.changeDisplayedContent(i)) }} > {i}</button>
</Browser>
</div>
);
}
return rows;
}
Output is always the length of the for loop.
Here is a clean version:
var rows = [];
for (var i = 0; i < Math.max(dropped2 && dropped2.length, dropped1 && dropped1.length); i++) {
rows.push(
<div key={i}>
<button key={"selectButton" + i} onClick={e => { console.log(i); } > {i} </button>
</div>
);
}
return rows;
This is the return function:
return (
<LeftPanel>
<div>
<h5 className="title is-5">Browse</h5>
</div>
<Column>
{getRows()}
</Column>
</LeftPanel>
)