I'm mapping an array and each has a button to delete it from the array :
const comments = [ 'Hey', 'Yo' ];
{comments.map((each, index) => {
return (
<div key={index}>
<button
onClick={() => {
console.log(comments.indexOf(each));
const id = comments.indexOf(each);
comments.splice(id, 1);
console.log(comments);
}}
>
Button
</button>
</div>);
}
When the button is clicked the item will be deleted and It console logs the array after deleting it and it works fine .
1
App.js:17 ["Hey"]
App.js:14 0
App.js:17 []
The only problem is the array which is displayed on the screen doesn't update in other words array displayed on the screen doesn't change after clicking the button and it still shows the whole array which has all the items .
How can I map through the changed version of the array or what am I doing wrong with mapping an array that doesn't display the changed version of it ?