I have a component:
constructor(props) {
super(props);
this.state = {
cards: []
};
}
Inside the this.state.cards array, for each card the data structure looks like:
{
movie: v,
ids: v.id,
goodDeal: `Cheapest price is ... at ...`
}
Now, I need to merge another cards(say cards2) array into my current state.cards array:
componentDidMount(){
cards2.map((v, i)=>{
var existedCard = this.state.cards.find((cv, ci)=>{
if(cv.code === v.code){
return cv;
}
})
if(existedCard){
existedCard.ids += v.id;
this.setState((prevState, pros)=> {
// then update the state, but how?
})
}
else {
// add the new card into the current cards array
}
})
}
As you can see, if I found an card(newCard) in card2 which is also in card1(which is the state.cards), then I add the newCard.id into the existedCard.ids. Then I want to call setState() to update the state.
But how?