I have this array that is formed from strings that I map through jsx and I want whenever a specific element is clicked to disappear by being removed from the array.
I tried achieving this by using slice() but I am doing something wrong and I would appreciate if you could help me.
This is how I tried to approach it.
export default function App() {
const arr = ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']
const removeMe = (index) => {
if(index > -1) {
arr.slice(index, 1);
}
return arr;
}
return (
<div className="App">
{arr.map((string, index) => (
<button
key={`string${index}`}
onClick={() => removeMe(index)}
>
{string}
</button>
))}
</div>
);
}
Also, here is a codeSandBox if it helps.