Material-table: Sorting by column is lost when data is updated

Created on 29 Jul 2020  路  11Comments  路  Source: mbrn/material-table

Describe the bug
Updating the data of a table resets any sorting.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://stackblitz.com/edit/react-9qzndg
  2. Sort a column
  3. Click 'Get API data'
  4. Observe the column losing its sorting

Expected behavior
I want the table to keep the sorting when updating the data.

Additional context
I think this behaviour was introduce at version 1.62.0 with this commit

bug

Most helpful comment

FYI I see the same thing but with a simple resize event...

All 11 comments

FYI I see the same thing but with a simple resize event...

Same issue here. Wondering if there is any update on this issue? Thank you!

I'm also having this problem, +1

Definitely seeing this issue too. Running material-table v1.68.0. ANY updates that cause a component render (like updating state via useState hook) will trigger this defect.

Same issue with v1.68.1

I also see this problem, especially annoying since it makes using sorting and selecting at the same time not usable when using state to track selection changes.

Also having this problem

@mbrn what are your thoughts on this issue ?

Hey guys I fixed this by using a workaround. Long story short, what I did was:

  1. Save the tableRef of the MaterialTable component
  2. Right before you update the data, check the tableRef.current.state. If the default sorting changed, save the orderBy and orderDirection values to a local ref.
  3. Also if the default sorting changed, pass these values to the parent component via a callback. The parent should then update the columns array that it passes to the component containing the MaterialTable component, with the new sorting values. By updating I mean updating the state of the columns. For this, you will need to save the columns to the component's state.

I hope this helps anyone. I know it might be a bit confusing but I'll let you with this for now. Maybe I find some time to put an example together somewhere.

I also have this issue, it's very annoying. Downgrading until this is fixed. Hope to see a fix soon.

Hey guys I fixed this by using a workaround. Long story short, what I did was:

  1. Save the tableRef of the MaterialTable component
  2. Right before you update the data, check the tableRef.current.state. If the default sorting changed, save the orderBy and orderDirection values to a local ref.
  3. Also if the default sorting changed, pass these values to the parent component via a callback. The parent should then update the columns array that it passes to the component containing the MaterialTable component, with the new sorting values. By updating I mean updating the state of the columns. For this, you will need to save the columns to the component's state.

I hope this helps anyone. I know it might be a bit confusing but I'll let you with this for now. Maybe I find some time to put an example together somewhere.

@gtupak , do you have any example for it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roseak picture roseak  路  3Comments

Mihier-Roy picture Mihier-Roy  路  3Comments

timrchavez picture timrchavez  路  3Comments

Likurg2010 picture Likurg2010  路  3Comments

victorwvieira picture victorwvieira  路  3Comments