Re render not happening on updated tableData when editingMode="table"
material-react-table version
1.3.2
Describe the bug and the steps to reproduce it
I am updating the tableData and calling setTableData([...tableData]) to re render the MaterialReactTable but table is not getting updated with new Data.
Minimal, Reproducible Example - (Optional, but Recommended)
const copyContent = async (row, index) => { console.log(row.row.original); await window.navigator.clipboard.writeText( JSON.stringify(row.row.original) ); };
const pasteContent = async (row, index) => {
const rowIndex = row.row.index;
const pasteData = await window.navigator.clipboard.readText();
const rowData = JSON.parse(pasteData);
tableData[rowIndex] = rowData;
setTableData([...tableData]); --> **This is not updating the MaterialReactTable with new data**
};
return (
<MaterialReactTable
columns={ columns }
data={ tableData }
editingMode="table"
enableEditing
enableRowNumbers
rowNumberMode="static"
enableRowActions
positionActionsColumn="last"
renderRowActions={ (row, index) => (
<Box>
<IconButton onClick={ () => copyContent(row, index) }>
<ContentCopyIcon />
</IconButton>
<IconButton onClick={ () => pasteContent(row, index) }>
<ContentPasteIcon />
</IconButton>
</Box>
) }
muiTableBodyCellEditTextFieldProps={ ({ cell }) => ({
// onBlur is more efficient, but could use onChange instead
onBlur: (event) => {
handleSaveCell(cell, event.target.value);
},
variant: 'outlined'
}) }
/>
);
Screenshots or Videos (Optional)
No response
Do you intend to try to help solve this bug with your own PR?
No response
Terms
- [X] I understand that if my bug cannot be reliably reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
- [X] I have verified I am using the correct peer dependency versions for my version of material-react-table listed in the installation instructions.
All of my own editing examples, which are supposed to do the same thing here, are working fine. You will need to create a full sandbox demoing the issue you are running into here.
My guess is that pasteContent is using the wrong data structures and not actually adding a new row to the table.
renderRowActions has this type signature: ({ cell, row, table }) => ReactNode, not the (row, index) one that you are trying to use.
I am having a similar problem. In my case, when I delete a row, it's always the last row that gets deleted (visually).
I found out that the problem is with the table editing mode. It seems to me that MRT_EditCellTextField is not handling cell value changes, as it only gets the cell value to initialize the value state (by using the useState hook).
I am having a similar problem. In my case, when I delete a row, it's always the last row that gets deleted (visually).
I found out that the problem is with the table editing mode. It seems to me that
MRT_EditCellTextFieldis not handling cell value changes, as it only gets the cell value to initialize thevaluestate (by using theuseStatehook).
Need to look into this. Yes makes sense that the input text state is the root of the problem.
I had a similar kind of Issue when deleting a row. I got all new records deleted and found that tableData was in the initial state in the renderRowActions delete function even though the table shows correct data. later I found that if we set displayColumnDefOptions prop even with an empty object issue got fixed. This is a strange way but I thought it will help to fix the issue.
@udithsc thanks a lot. I had the exact same problem and it was driving me nuts. I confirm that setting the displayColumnDefOptions prop solves the issue
Having the same issue, even when setting the prop <MaterialReactTable displayColumnDefOptions={{}} editingMode="table" ... />, I'm using material-react-table version 1.8.5.
I switched to the editingMode="cell" type instead of editingMode="table"; I guess I'll change back to table once this is resolved.
The problem is a unique key for each row is the index, which is the default behavior. . Therefore you should tell the material-react-table about the unique key for each row by using getRowId method
For example: if the unique key of the row is userId, you can use the following code:
<MaterialReactTable
getRowId={(originalRow) => {return originalRow.userId }}
editingMode="table"
{...rest}
/>
What I think is happening is that displayColumns is being memoized. This memoization is deep, it includes MRT_ToggleRowActionMenuButton > MRT_EditActionButtons > handleSave > onEditingRowSave. It'll cache the references used in onEditingRowSave and keep using those unless something changes in the useMemo dependency array.
The prop "displayColumnDefOptions" is a dependency, by setting to an empty inline object you're essentially disabling the useMemo, since an inline object will be a new reference every render, it'd work with any other dependency that's an object (for example: localization).
Here's a codesandbox to see what useMemo does to states you want to use inside your functions.
The reason the demo page for editing works is that the original data reference is being modified before it's copied.
This might be all the way fixed in v2.9.0 as columns are no longer memoized internally except during resizing and dragging events.
Hello, this happens for me, too, even on 2.13.1. I have 2 cases where this doesn't work, both related to updating the table data:
- deleting a row: the values from the deleted row show up in the row that replaces it
- updating a cell in a different column than the one being changed. Eg: based on the value inserted in column A, change column B to X. The X value is simply not displayed.
The only solution that fully worked for me was the one mentioned by @kitravee I added a uuid for each row and changed it when i had to update the value in a different cell.
Thank you
Closing, as the main solution is to just use a proper row id with the getRowId table option. In MRT v3, I've also added an extra key prop internally to force a remount when the row id changes or the row index changes.