1

I'm encountering an error in a React app using MUI X Data Grid component when attempting to delete rows. Here is the full code snippet:

import React, { useState } from 'react';
import { DataGrid } from '@mui/x-data-grid';
import IconButton from '@mui/material/IconButton';
import DeleteSharp from '@mui/icons-material/DeleteSharp';
import Tooltip from '@mui/material/Tooltip';

const TestComponent = () => {
  const [draftColumns, setDraftColumns] = useState([]); // State to hold DataGrid rows

  // Function to delete rows
  const handleDraftColumnDelete = async row => {
    const result = draftColumns.filter(col => col.index !== row.index);    
    setDraftColumns(result);
  };

  const columns = [
    // ... other columns setup
    {
      field: 'actions',
      headerName: 'Actions',
      width: 130,
      headerAlign: 'center',
      align: 'center',
      renderCell: ({ row }) => {
        if (row.column && row.column.value && !row.column.value.isRequired) {
          return (
            <Tooltip title='Delete'>
              <IconButton size='small' onClick={() => handleDraftColumnDelete(row)}>
                <DeleteSharp color='error' />
              </IconButton>
            </Tooltip>
          );
        } else return <></>;
      },
    },
  ];

  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid
        columns={columns}
        rows={draftColumns}
        getRowId={row => row.index}
        disableSelectionOnClick
      />
    </div>
  );
};

export default TestComponent;

The issue arises when I attempt to delete a row using the handleDraftColumnDelete function. Clicking the delete button triggers the function, but I receive an error "Uncaught Error: No row with id #11 found". I suspect there might be an issue with how I am identifying or deleting rows.

Any help, suggestions, or guidance would be greatly appreciated. Thank you!

1 Answer 1

0

I faced that issue when I was deleting rows.

Using the fact that selection worked as a clue, I resolved it as follows.

Let's assume for now that the uniqueKey is passed as a prop.

<DataGrid
    rows={list}
    getRowId={(row) => {
        return row[uniqueKey];
    }}
    isRowSelectable={(row) => {
        return list.map(item => item[uniqueKey]).includes(row.id);
    }}
/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.