Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display loading info based on clicked delete button in reactjs

The code below is working fine by deleting record.

Now Am trying to display text Loading. deleting in progress… only for that particular button being clicked.

Here is my issue, when I click on a single record button, i can see the text Loading. deleting in progress… displayed in all the record instead of showing for that single particular button i clicked.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is the screenshot

I have tried using id to indicated the clicked row button but cannot get it to work as per below
I set this withing the handleDeleteRow() function

this.setState({loading_id: false});
this.setState({loading_id: true});

I set this at render

{loading_person.id && <span>Loading. deleting in progress...</span>}

here is the code

import React from "react";
import { render } from "react-dom";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      
    };
  }

  componentDidMount() {
    this.setState({
      data: [
        { id: "1", name: "user 1" },
        { id: "2", name: "user 2"},
        { id: "3", name: "user 3"}
      ]
    });

 this.handleDeleteRow = this.handleDeleteRow.bind(this);
  }



handleDeleteRow = (id) => {
    this.setState({loading: true});
alert(id);
    let data = [...this.state.data];
    data.splice(id, 1);
    this.setState({ 
      data: data
    });
    this.setState({loading: false});
  }




  render() {

const {loading} = this.state;
    return (
      <span>
        <label>
          <ul>
            {this.state.data.map(person => (
              <li key={person.id}>
                {person.name}
                <br />

<button onClick={() => this.handleDeleteRow(person.id)} style={{color:'red'}}>Delete</button>
{loading && <span>Loading. deleting in progress...</span>}
              </li>
            ))}
          </ul>
        </label>
      </span>
    );
  }
}


export default App;

>Solution :

You can store id of the object being deleted and enable loading indicator based on stored id.
Instead of this.setState({loading: true}), use this in handleDeleteRow:

this.setState({deletedId: id});

And in render:

{this.state.deletedId===person.id && Loading. deleting in progress…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading