FROM CACHE - en_header

Polaris React Index Table component: How to clear selection from code.

belazaras
New Member
2 0 3

I have code to delete some items from the table but the selection will not update. How do I trigger a recalculation of selectedResources from useIndexResourceState?

Replies 3 (3)
Menna2
New Member
1 0 0

Can you please tell me if you have figured it out?

ratmil
New Member
2 0 1

This is what I did:

while(selectedResources.length > 0) {
       selectedResources.pop();
 }
 
Then I changed some state that would cause the Index Table to re-render, rows for example.
olavoasantos
Shopify Staff
Shopify Staff
19 4 13

Hi @belazaras. How are you today?

I'm Olavo from Shopify. Thank you for reaching out.

If I understood correctly, you're using an `IndexTable` and deleting multiple selected entries but the `selectedResources` from `useIndexResourceState` isn't reflecting all changes, correct?

If so, an easy way to get around this is to trigger the `handleSelectionChange` function with a deselect action once you deleted your entries. If you look at the code for this hook you'll see that the `handleSelectionChange` function expects the `SelectionType` (which is an enum) and a boolean (that represents if this action is selecting or de-selecting items). So, if you want to deselect everything, you'd call it with `handleSelectionChange(SelectionType.All, false)` (because you don't have access to enum you can just write `"all"`).

In practice you'd have something in the lines of:

function onDelete(items) {
  // ... Your logic to delete the items ...
  handleSelectionChange('all', false) // <~~ This will trigger the recalculation
}

I hope this helps. Let me know if you have any more questions.