I have some elements with an onclick handler, which should pass the value of the variable it had, when the elements were rendered. Unfortunately they only pass the value that the variable has at the point of clicking.
class Application extends React.Component {
constructor(props){
super(props)
this.state = {
clickedValue: 0,
array: [1,2,3,4,5,6,7,8,9,10]
}
}
changeHeadline(number){
this.setState({clickedValue: number})
}
render() {
let paginationCounter = 0;
return <div>
<h1>{this.state.clickedValue}</h1>
{
this.state.array.map((element, index) => {
if ((index + 1) % 2 === 0) {
paginationCounter += 1;
return (
<button
onClick={() => this.changeHeadline(paginationCounter)}
>
{paginationCounter}
</button>
)
}
})
}
</div>;
}
}
I also created a pen for it: https://codepen.io/anon/pen/jGKzzz My intended behavior in this pen is: The headline text should change to the number the button has on it, when the button is clicked.
I'd greatly appreciate your help. Thanks in advance!