react hooks delete item from array code example
Example: react hooks delete item from array
import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = () => {
const defaultList = [
{ name: "ItemOne" },
{ name: "ItemTwo" },
{ name: "ItemThree" }
];
const [list, updateList] = useState(defaultList);
const handleRemoveItem = (e) => {
const name = e.target.getAttribute("name")
updateList(list.filter(item => item.name !== name));
};
return (
{list.map(item => {
return (
<>
x
{item.name}
>
);
})}
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(, rootElement);