Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTable: sortFunction retains stale data #2249

Closed
VsevolodGolovanov opened this issue Aug 21, 2021 · 5 comments · Fixed by #3027
Closed

DataTable: sortFunction retains stale data #2249

VsevolodGolovanov opened this issue Aug 21, 2021 · 5 comments · Fixed by #3027
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@VsevolodGolovanov
Copy link

I'm submitting a ... (check one with "x")

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://forum.primefaces.org/viewforum.php?f=57

Codesandbox Case (Bug Reports)
https://codesandbox.io/s/primereact-datatable-sortfunction-retains-stale-data-updated-u5esv

Current behavior
A custom sortFunction is not passed in data, so you have to keep track of it yourself. But even though my sortFunction gets recreated with new data, the sortFunction instance that ends up being used is old and remembers stale data.

Expected behavior
The recreated up-to-date sortFunction instance is used for sorting.

Minimal reproduction of the problem with instructions
Open linked codesandbox.
Click the "New data" button.
Observe the table showing updated data.
Now click the table header to enable sorting.
Click the "New data" button.
Observe the table showing stale data.

In the sandbox console you can see that createSortFunc was called with up-to-date data, yet the sortFunction that got invoked after that rememberd stale data.

image

Please tell us about your environment:

  • React version:

17.0.2

  • PrimeReact version:

6.5.1

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]
@VsevolodGolovanov
Copy link
Author

This is a repeat of #1705 with an updated codesandbox. Don't know why you'd want duplicates, but here it is.

@mertsincan
Copy link
Member

Hi,

I think this is related to demo. In the last phase, the value is [7, 8, 9] in DataTable component. But, it is [4,5,6] in your createSortFunc and old value remains in callback. Please try;
https://codesandbox.io/s/primereact-datatable-sortfunction-retains-stale-data-updated-forked-5yw5y?file=/src/index.js

Best Regards,

@mertsincan mertsincan added the Resolution: Needs More Information More information about the issue is needed to find a correct solution label Nov 7, 2021
@VsevolodGolovanov
Copy link
Author

VsevolodGolovanov commented Nov 11, 2021

@mertsincan
As the screenshot in description shows, sort function is recreated with actual data (7,8,9). But the table uses older function with 4,5,6.
You're just working around this issue by passing a function instead of the value.

Also as I stated in #1705:

IMHO sortFunction should be passed data by the DataTable. This will also allow for filtering the data before sorting.

Maybe even change it to be a predicate. So that the sortFunction wouldn't be able to replace data at all.

@VsevolodGolovanov
Copy link
Author

VsevolodGolovanov commented Dec 10, 2021

You're just working around this issue by passing a function instead of the value.

And you can't even do that with a functional component, since you don't have this.state.data to reference - you just have data and your getData function would see the same outdated value that it closed over on a previous render.

Functional version of my reproducer: https://codesandbox.io/s/primereact-datatable-sortfunction-retains-stale-data-updated-2-ykv5w
Functional getData failure proof: https://codesandbox.io/s/primereact-datatable-sortfunction-retains-stale-data-updated-2-getdata-tte7x

@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Resolution: Needs More Information More information about the issue is needed to find a correct solution labels Apr 19, 2022
@melloware
Copy link
Member

This is fixed in 8.2.0

@melloware melloware self-assigned this Jul 6, 2022
@melloware melloware added this to the 8.2.0 milestone Jul 6, 2022
@melloware melloware linked a pull request Jul 6, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants