I'm trying to accomplish two things. One, onClick, I am adding values to a state property, cardPairs. I made a copy of the state object and used concat to add a value. It takes me three clicks to create a match. The first time I click, I get the empty array, then on two subsequent correct clicks, I'll have a match. Using push doesn't work on my copy, I get a "could not consume" error and when trying push, I get "object is not extensible". How can I match on two clicks? The second issue is removing the matched items from the UI.
class CardCollectionTwo extends Component {
constructor(props) {
super(props);
this.state = {
hidden: false,
cardPairs: []
};
this.compareCards = this.compareCards.bind(this);
}
compareCards(e) {
const pairs = Object.freeze(this.state.cardPairs)
console.log(pairs)
this.setState({ cardPairs: pairs.concat(e.target.value) });
console.log(this.state);
if(pairs[0] === pairs[1] && pairs.length!== 0){
console.log('MATCH')
}else if(pairs.length === 2){
this.setState(prevState => ({ cardPairs: [] }))
}
}
render() {
let cards = this.props.cardsEasy["cards"].map((item, index) => {
return (
<button
style={btn}
value={item}
id={item}
onClick={this.compareCards}
key={item + index}
>
{item}
</button>
);
});
return <CardTwo cards={cards} />;
}
}
export default CardCollectionTwo;