I'm trying to change the value of like within the cardArr objects while using a map() to list each obj.
Below is my code. Current, it works, but every object shares the same counter since it's not calling from the object. I understand that I'm currently just calling the object property stored in state, but how do I edit the like in each object using a map function?
import React, { Component } from 'react';
const cardArr = [
{
id: 1,
text: "Hey this is a test.",
img: " ",
like: 0
},
{
id: 2,
text: "If this works I'll call it a day.",
img: " ",
like: 0
},
{
id: 3,
text: "I'll drink a lot of beer.",
img: " ",
like: 0
},
{
id: 4,
text: "Cheers",
img: " ",
like: 0
}
]
export class Card extends Component {
constructor(props) {
super(props);
this.state = {
like: 0,
show: true
};
// this.handleClick = this.handleClick.bind(this);
}
IncrementItem = () => {
this.setState({ like: this.state.like + 1 });
}
DecreaseItem = () => {
this.setState({ like: this.state.like - 1 });
}
// handleClick(e) {
// e.preventDefault();
// this.IncrementItem();
// }
// handleClick(e) {
// e.preventDefault();
// this.DecreaseItem();
// }
render() {
const cardList = (cardArr.map((card) =>
<ul>
<li>
<div key={card.id}>
{card.text};
<img src={card.img}/>
<p>Like Button</p>
<button onClick={this.handleClickAdd}>Like</button>
<p>Dilike Button</p>
<button onClick={this.DecreaseItem}>Disike</button>
<p>Likes: {this.state.like}</p>
</div>
</li>
</ul>
));
return(
<div id='card'>
{cardList}
</div>
)
}
}