I tried to find the current modification of setting a new element into an array in react.js with 'setState'. Everything I tried just didn't add the new element to the array, it just replaced it with a new element.
const [unplannedCards, setUnplannedCards] = useState([card]);
setUnplannedCards([{...unplannedCards}, { ...card }]);
I also tried some other similar modifications but nothing helped.
can someone see where I got wrong?
CodePudding user response:
You can add a new card in your array this way :
const [unplannedCards, setUnplannedCards] = useState([card]);
setUnplannedCards([...unplannedCards, card]);
CodePudding user response:
You can try this way which is the fastest
setUnplannedCards(unplannnedCards.concat(card))
