简体   繁体   English

删除按钮在我的 React 应用程序上不起作用

[英]Delete button doesn't work on my react app

I created delete function for delete button but it doesn't work.我为删除按钮创建了删除 function,但它不起作用。 I looked at the example and the code is almost identical but somehow it's not working out for me.我查看了示例,代码几乎相同,但不知何故它对我不起作用。 Are there any suggestions?有什么建议吗?

import { useState } from "react";
import "./userList.scss";
import { DataGrid } from "@mui/x-data-grid";
import { userColumns, userRows } from "../../datatablesource";
import { Link } from "react-router-dom";
const Datatable = () => {
  const [data, setData] = useState(userRows);

  const deleteData = id => {
    setData(data.filter(item => item.id !== id));
  };

  const actionColumn = [
    {
      field: "action",
      headerName: "Action",
      width: 200,
      renderCell: params => {
        return (
          <div className="cellAction">
            <Link to="/users/profile" style={{ textDecoration: "none" }}>
              <button className="viewButton">View</button>
            </Link>
            <button className="deleteButton" onClick={() => deleteData(params.row.id)}>
              Delete
            </button>
          </div>
        );
      }
    }
  ];
  return (
    <div className="datatable">
      <DataGrid rows={userRows} columns={userColumns.concat(actionColumn)} pageSize={5} rowsPerPageOptions={[5]} checkboxSelection disableSelectionOnClick />
    </div>
  );
};

export default Datatable;

Here is the data for userRows.这是 userRows 的数据。 It has it's id property which i use it for delete function.它有它的 id 属性,我用它来删除 function。

export const userRows = [
  {
    id: 1,
    username: "Snow",
    img: "https://images.pexels.com/photos/1820770/pexels-photo-1820770.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500",
    status: "active",
    email: "1snow@gmail.com",
    age: 35
  },
  {
    id: 2,
    username: "Jamie Lannister",
    img: "https://images.pexels.com/photos/1820770/pexels-photo-1820770.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500",
    email: "2snow@gmail.com",
    status: "passive",
    age: 42
  }
];

Here is the data for userColumns:这是 userColumns 的数据:

export const userColumns = [
  {
    field: "id",
    headerName: "ID",
    width: 70
  },
  {
    field: "user",
    headerName: "Username",
    width: 250,
    renderCell: params => {
      return (
        <div className="cellWithImg">
          <img className="cellImg" src={params.row.img} alt="avatar" />
          {params.row.username}
        </div>
      );
    }
  },
  {
    field: "email",
    headerName: "Email",
    width: 280
  },
  {
    field: "age",
    headerName: "Age",
    width: 100
  },
  {
    field: "status",
    headerName: "Status",
    width: 160,
    renderCell: params => {
      return <div className={`cellWithStatus ${params.row.status}`}>{params.row.status}</div>;
    }
  }
];

First : use state value (data) in rows props in the Datagrid第一:在 Datagrid 的行道具中使用 state 值(数据)

<DataGrid rows={data} columns={userColumns.concat(actionColumn)} pageSize={5} rowsPerPageOptions={[5]} checkboxSelection disableSelectionOnClick />

second :第二

Q) How to update state value in react js? Q)如何在 React js 中更新 state 值?

In delete function you are updating the state value and to update the state this approach is not the correct one.在删除 function 中,您正在更新 state 值并更新 state 这种方法是不正确的。 correct way to update the state is this更新 state 的正确方法是这样的

  1. use callback function where you get the previous value of the state使用回调 function 获得 state 的先前值

  2. then filter the array as you have done然后按照你所做的那样过滤数组

  3. then destructure the array to create the clone of that array to update the state value然后解构数组以创建该数组的克隆以更新 state 值

    const deleteData = (id) => { setData((d) => { return [...d.filter((item) => item.id;== id)]; });}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM