How to correct modificat of state arrays in React.js

Viewed 50

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?

1 Answers

You can try this way which is the fastest

setUnplannedCards(unplannnedCards.concat(card))
Related