material-table icon indicating copy to clipboard operation
material-table copied to clipboard

The table is fully rerendered on parent state update since 1.53.0

Open sebastiendan opened this issue 7 years ago • 24 comments

Describe the bug I updated to 1.53.0 and noticed the table was fully rerendered when the parent component state was updated. This was a big deal in my situation: I have an EventSource that receives data pushed by the server and renders new data in new rows. Because of this new behaviour each data push (followed by a state update in the parent) led the table to get back to init configuration (first page, default page size, etc.).

Is this somehow the intended behaviour?

To Reproduce Steps to reproduce the behavior:

  1. Have a paginated table with multiple pages of rows
  2. Update the parent state with new values (e. g. update a boolean to its opposite value on an interval)
  3. Change page or page size
  4. See the table getting back to its init configuration on rerender

Expected behavior Before 1.53.0, you can navigate through the pages and see new rows appear. You can also change any table UI setting without loosing it,

Desktop (please complete the following information):

  • OS: Windows
  • Browser: Chrome
  • Version: 78.0.3904.70 (Official Build) (64-bit)

sebastiendan avatar Oct 29 '19 04:10 sebastiendan

I'm having the same issue. New array passed into data is reseting pagination and the search field.

TJHdev avatar Oct 29 '19 09:10 TJHdev

We're having this issue too. As a temporary fix, downgrading to version 1.52.0 solved the problem for us.

I'm looking at the release notes for v1.53.0 trying to figure out which change introduced this behaviour. Here's the compare view for v1.52.0...v1.53.0

nelstrom avatar Oct 31 '19 15:10 nelstrom

I've found what might be a possible cause.

In src/material-table.js, the setDataManagerFields() function now includes this line:

this.dataManager.changeSearchText(props.options.searchText);

In src/utils/data-manager.js, the changeSearchText() function looks like this:

  changeSearchText(searchText) {
    this.searchText = searchText;
    this.searched = false;
    this.currentPage = 0;
  }

I haven't had a chance to dig deep into this and figure out how often the changeSearchText() function is being called, but I thought the fact that it's setting this.currentPage = 0 might be a possible cause for this issue.

nelstrom avatar Oct 31 '19 16:10 nelstrom

@nelstrom I came to the same conclusion. However, I hadn't dug deep enough to see that paging was being reset as well. My (probably duplicate) issue is #1272

re-anderson avatar Oct 31 '19 17:10 re-anderson

I'm now observing better behavior here, things like search text and current page are preserved on re-render, but the chosen "pageSize" and an columns selected/deselected using "columnsButton" are reset to the initial state.

I'm on 1.54.1.

jlucier avatar Nov 12 '19 19:11 jlucier

I'm having a similar issue. I have a material-table with grouping turned on inside a tabbed UI via material-ui/Tabs, if I change tabs and come back the table has lost grouping state. Even if I modify my TabPanel control to not actually hide I still get the re-render and loss of state when selecting tab controls.

function TabPanel(props: TabPanelProps) {
    const { children, value, index, ...other } = props;
    return (
        <Typography
            component="div"
            role="tabpanel"
            // hidden={value !== index}  <--- DISABLE HIDING
            id={`nav-tabpanel-${index}`}
            aria-labelledby={`nav-tab-${index}`}
            {...other}>
            <Box p={3}>{children}</Box>
        </Typography>
    );
}

Search and Sort will maintain themselves. However anything changed via Draggable or Grouping (drag and drop) will revert on parent element change.

AndyWorral avatar Dec 18 '19 22:12 AndyWorral

@AndyWorral Making the columns array passed to material-table part of the parent component's state appears to fix the issue you're having with maintaining grouping state, which will work if you're not doing any dynamic modification of said columns array.

zxqj avatar Feb 19 '20 19:02 zxqj

having the same issue, keep pushing new data to the table and all selection and state is lost in each update. isn't there like a key property that we can use?

omridevk avatar Mar 15 '20 06:03 omridevk

I'm having the same issue and now looking into this.

Getting the demo app in the repo up and running (after some minor issues), I could not reproduce this problem.

The demo app was a class component, so I formed the hypothesis, that it had something to do how functional components or hooks caused rerenders.

So I tested this hypothesis, and lo and behold: When changing a useState hook in a parent component containing a material-table, the filtering, and selection is reset.

So any ideas why this is happening?

Abekonge avatar Apr 22 '20 13:04 Abekonge

@Abekonge Found a way: you can pass to useTable the following:

useTable({getRowId: React.useCallback(row => {use row key that is persistent}, []})

that solved the issue for me, now every row/cell had a prefix with my special key in it so as long as my rows themselves kept the same key, the state would not updated if the key hasn't changed. Hope that helps.

Thanks for looking!

But I'm confused - in your code you are using react-table which is a completely different table-library. Or am I misunderstanding something here?

Abekonge avatar Apr 22 '20 13:04 Abekonge

baaah sorry man, I had this issue with Material UI, so instead I have moved to using react-table with Material UI Table as the UI layer. Sorry for misleading, I was sure this thread was in react-table repository. My bad, sorry for the confusion. But if you want, you can use https://www.npmjs.com/package/react-table with Material UI pretty straight forward React-table has soo many useful features built in, and as it just provides functionality via Hooks, you can use Material UI table to render the UI, but use the selection, pagination, and other table logic is being handled by React table library.

omridevk avatar Apr 22 '20 13:04 omridevk

Could be an overkill for your project, but I found that react-table with Material UI table works really really well.

omridevk avatar Apr 22 '20 13:04 omridevk

I'm having the same issue and now looking into this.

Getting the demo app in the repo up and running (after some minor issues), I could not reproduce this problem.

The demo app was a class component, so I formed the hypothesis, that it had something to do how functional components or hooks caused rerenders.

So I tested this hypothesis, and lo and behold: When changing a useState hook in a parent component containing a material-table, the filtering, and selection is reset.

So any ideas why this is happening?

Ok I was wrong. It was nothing to do with class vs functional component.

But similar to zxqj 's answer above. If you lift the data state up to the parent, the selection state does not get reset by changing parent state.

Filtering state on the other hand is still reset. I'm looking into this now:

(update) Filtering state can be kept by lifting the columns content up into the parent state, similar to what zxqj's asserted for grouping state.

so

WORKAROUNDS:

Problem: Selection gets reset when changing parent state Solution:

  • Lift the contents of the data prop into parent state, to preserve selection state in material table.

Problem: Filter-settings and grouping (and possible other things) gets reset when changing parent state Solution:

  • Lift the contents of the columns prop into parent state, to preserve filter-settings and grouping

Abekonge avatar Apr 23 '20 09:04 Abekonge

Could be an overkill for your project, but I found that react-table with Material UI table works really really well.

I've changed to react-table after looking into it, because of your comment. And I love it! So comments in the wrong place can be helpful too! ;-)

Abekonge avatar Jun 05 '20 13:06 Abekonge

I am having the same issue with 1.65.0 version. Anyone knows if there is a temporary solution until this is resolved?

baloian avatar Aug 24 '20 01:08 baloian

I'm using setState in onSelectionChange

onSelectionChange={(rows) => {
                    setState({ ...state, selectedTitles: rows });
                 }}

@nelstrom I downgraded to 1.52.0 but the problem isn't solved.

@Abekonge I lifted the columns prop data into parent state (not a state variable actually), it's solved.

Thank you guys!

s-kris avatar Sep 23 '20 15:09 s-kris

I'm having the same issue and now looking into this. Getting the demo app in the repo up and running (after some minor issues), I could not reproduce this problem. The demo app was a class component, so I formed the hypothesis, that it had something to do how functional components or hooks caused rerenders. So I tested this hypothesis, and lo and behold: When changing a useState hook in a parent component containing a material-table, the filtering, and selection is reset. So any ideas why this is happening?

Ok I was wrong. It was nothing to do with class vs functional component.

But similar to zxqj 's answer above. If you lift the data state up to the parent, the selection state does not get reset by changing parent state.

Filtering state on the other hand is still reset. I'm looking into this now:

(update) Filtering state can be kept by lifting the columns content up into the parent state, similar to what zxqj's asserted for grouping state.

so

WORKAROUNDS:

Problem: Selection gets reset when changing parent state Solution:

  • Lift the contents of the data prop into parent state, to preserve selection state in material table.

Problem: Filter-settings and grouping (and possible other things) gets reset when changing parent state Solution:

  • Lift the contents of the columns prop into parent state, to preserve filter-settings and grouping

can someone give a small example on how one would go about liftig the the contents of the data prop into the parent state? I am fairly new to react and I would love to see how that works =)

geissmannsbin avatar Oct 28 '20 16:10 geissmannsbin

I'm having the same issue and now looking into this. Getting the demo app in the repo up and running (after some minor issues), I could not reproduce this problem. The demo app was a class component, so I formed the hypothesis, that it had something to do how functional components or hooks caused rerenders. So I tested this hypothesis, and lo and behold: When changing a useState hook in a parent component containing a material-table, the filtering, and selection is reset. So any ideas why this is happening?

Ok I was wrong. It was nothing to do with class vs functional component. But similar to zxqj 's answer above. If you lift the data state up to the parent, the selection state does not get reset by changing parent state. Filtering state on the other hand is still reset. I'm looking into this now: (update) Filtering state can be kept by lifting the columns content up into the parent state, similar to what zxqj's asserted for grouping state. so

WORKAROUNDS:

Problem: Selection gets reset when changing parent state Solution:

  • Lift the contents of the data prop into parent state, to preserve selection state in material table.

Problem: Filter-settings and grouping (and possible other things) gets reset when changing parent state Solution:

  • Lift the contents of the columns prop into parent state, to preserve filter-settings and grouping

can someone give a small example on how one would go about liftig the the contents of the data prop into the parent state? I am fairly new to react and I would love to see how that works =)

Here, I simplified the code for readability

function CustomTable(props) {
  const { data, loading } = fetchInitialData();
  const tableData = data ?.tableData || []; // we need to do this because columns are generated dynamically

  if (loading) {
    return (
      <Card>
        <Skeleton count={20} />
      </Card>
    );
  }

  return (
        <MaterialTable
          {...props}
          data={(query) =>
            new Promise((resolve, reject) => {
              const remoteDataObject = fetchRemoteData(someParams);
              resolve(remoteDataObject);
            })
          }
          columns={props.columns || generateColumns(tableData[0] || [])}
        />
  );
}

export default CustomTable;

s-kris avatar Oct 28 '20 16:10 s-kris

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Jan 26 '21 16:01 stale[bot]

This is still an issue.

roseak avatar Mar 24 '21 06:03 roseak