Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
React table pre select rows

I'm trying to preselect rows in my table but the table won't refresh unless there are changes to the actual data itself. Is there a method to reinit the table that doesn't involve changing the data?

It's also completely possible that my method for approaching this requirement is wrong and there may be a better way? I've created an example sandbox here:

https://codesandbox.io/s/mock-preselected-rows-data-t36nl?file=/src/App.js

In this you can see I have a mock response from my server for determining what rows should be selected. I'm then grabbing the data to compare to see if any of the items from the mock response exist in the data and if so push them to a new obj which is then fed into the intialState for selectedRowIds

Any guidance appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Seems your work is all working. The short answer to your question.

As long as you want the user see something, in a React way, it needs to be contained in a state, or state derivative. In your case, it's a cell data wrapped in row and in a table.

So you can't avoid selecting it without touching the data. Unless you don't want user see the change.

Although the checkbox doesn't seem to be part of the original data stream, when you develop on it, you have to make it part of the data. To be honest, it's easy you make it part of the data, because by the time you want to refresh the table, ex. selecting or de-selecting, or deleting a row, you want everything refreshed. Unfortunately it's very difficult to do local refresh with a table in React. It's possible, but very difficult, because most of the design is based on either prop or context.

about 4 years ago · Juan Pablo Isaza Report

0

You can also refactor your handleSelectedRows function.

// Find row ids and compare them with our 'preSelectedTheseItems' array.
  const handleSelectedRows = () => {
     const preIds = preSelectTheseItems.map(item => item.collectibleId)
     return data?.filter((collectibleRow, index) => preIds.includes(collectibleRow.collectibleId));
  };

Example : codesandbox

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!