I
I
Insolation2018-04-27 22:36:37
React
Insolation, 2018-04-27 22:36:37

How to catch the id of a row and delete it?

Hello.
I have a table of products, the products themselves are an array. On a click on the Delete button, I need to delete a row.
I decided to do this:

  • 1. Making a delete method (deleteRow)
  • 2. We will delete it like this - we catch the id of the row, remove the element from the array and rewrite the state.

The problem is that I'm confused as to how best to:
  • 1. Catch row id
  • 2. Make the deleteRow method (now it seems to me that it's just nonsense)

Here is the code itself - https://codesandbox.io/s/j1l0y4rmw9

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-04-27
@Insolation

Pass the index of the element to be removed to the delete method, for example:

deleteRow(index) {
  const productsList = [...this.state.productsList];
  productsList.splice(index, 1);
  this.setState({
    productsList
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question