📌  相关文章
📜  反应钩子从数组中删除项目 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:02:27.730000             🧑  作者: Mango

代码示例1
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);