DEV Community

Srikanth Kyatham
Srikanth Kyatham

Posted on

2

How we solved Gnarly bug with React table + useGlobalFilter + useRowSelect

Application

  • We have react table showing data related to customers
  • We have external filters, to filter out customer based on a selected filter criteria

Image description

Usage which cause issue

  • Whenever the master checkbox is checked, we get the list of selected indices from the react table.
  • The selected indices are an array of strings like ["0", "1"..]
  • We parse the string to number and then iterate over given customers and filter the customer which match the indices given us
  • This was working fine till the global filter came in to play.

Image description

With Global filter

  • Lets say we have filter which filters the customers based on the age
let customers = [
  {
    name: "name1",
    age: 60,
  },
  {
    name: "name2",
    age: 20
  }
]
Enter fullscreen mode Exit fullscreen mode
  • We have picked the age-group [0-20]
  • Since the external data has changed the globalFilter function is invoked and after the filter is applied the result would be
customers = [
  {
    name: "name2",
    age: 20
  }
]
Enter fullscreen mode Exit fullscreen mode

Now if we select the master-checkbox of the react table, the selected indices returned is ["1"] not ["0"], this minor details caught us off guard. Meaning the selected customers is always based on the original data set which is fed to the react table.

Image description

Code sandbox link for the above code - https://codesandbox.io/s/unruffled-dubinsky-lrxixo?file=/src/App.js

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay