I have a list of board numbers. This list I am exporting and mapping into it to create the elements how I want. Now, I need each of these elements to have its own count, and when I click on one of them, I need to append ◘
symbol to the clicked element
The problem is that onClick
event is updating all of my elemtents.
Here is a live demo, and also below is my current code:
export class App extends React.Component {
constructor(props) {
super(props);
this.state = {
count: ''
}
}
appendChild(id) {
console.log(id) // I am successfuly getting the id of the clicked element
// This code should update only the id that I have clicked
this.setState(prevState => {
return {
count: prevState.count + '◘'
}
});
}
render() {
const boardItems = BoardNumbers.map((item) =>
<div key={item.id} className={(item.color === "black") ? 'black-item' : 'red-item'}>
<div onClick={this.appendChild.bind(this, item.id)} className="value">{item.id}
{this.state.count}
</div>
</div>
);
return (
<>
{boardItems}
</>
)
}
}