Saif Ejjilali
Saif Ejjilali

Reputation: 107

refresh table after action in react

Hi i tried to make a MERN app

enter image description here

but there's a thing, when i udate or delete there no refresh of the page, how can i do it? i create an index where i list all my entries

    // index.component.js

import React, { Component } from 'react';
import axios from 'axios';
import TableRow from './TableRow';

export default class Index extends Component {

  constructor(props) {
      super(props);
      this.state = {business: []};
    }
    componentDidMount(){
      axios.get('http://localhost:4000/business')
        .then(response => {
          this.setState({ business: response.data });
        })
        .catch(function (error) {
          console.log(error);
        })
    }
    tabRow(){
      return this.state.business.map(function(object, i){
          return <TableRow obj={object} key={i} />;
      });
    }

    render() {
      return (
        <div>
          <h3 align="center">Liste des billets</h3>
          <table className="table table-striped" style={{ marginTop: 20 }}>
            <thead>
              <tr>
                <th>Nom Prénom</th>
                <th>Poste</th>
                <th>Téléphone</th>
                <th colSpan="2">Action</th>
              </tr>
            </thead>
            <tbody>
              { this.tabRow() }
            </tbody>
          </table>
        </div>
      );
    }
  }

here's my table with the options

    // TableRow.js

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';

class TableRow extends Component {

  constructor(props) {
        super(props);
        this.delete = this.delete.bind(this);
    }
    delete() {
        axios.get('http://localhost:4000/business/delete/'+this.props.obj._id)
            .then(console.log('Deleted'))
            .catch(err => console.log(err))
    }
  render() {
    return (
        <tr>
          <td>
            {this.props.obj.nomPrenom}
          </td>
          <td>
            {this.props.obj.posteEntreprise}
          </td>
          <td>
            {this.props.obj.numeroTel}
          </td>
          <td>
            <Link to={"/edit/"+this.props.obj._id} className="btn btn-primary">Editer</Link>
          </td>
          <td>
            <button onClick={this.delete} className="btn btn-danger">Supprimer</button>
          </td>
        </tr>
    );
  }
}

export default TableRow;

here what i tried here , my delete and my update work but the modification is confirmed only when i refresh the page.

Upvotes: 1

Views: 17098

Answers (3)

nima moradi
nima moradi

Reputation: 2618

in your delete function you should update state

     delete() {
        axios.get('http://localhost:4000/business/delete/'+this.props.obj._id)
            .then(()=>{
                        let list=this.state.business.filter((item)=>return (item.id===this.props.obj._id))
                        this.setState({business:list})}
            .catch(err => console.log(err))
    }

idk how to delete in your case cause i don't know list but it should be similar

Upvotes: 1

Darkilen
Darkilen

Reputation: 571

You must update your data in the callback of your delete/edit request.

Currently, you just log that the deletion was successful.

Upvotes: -1

dan
dan

Reputation: 1364

It looks like you only get the business data from the API in the componentDidMount of the Index Component.

Instead, you should have a method that gets the data from the API and call it both in the componentDidMount and in the .then of the delete method in TableRow.

For example:

componentDidMount(){
  this.getBusinessData();
}
getBusinessData() {
    axios.get('http://localhost:4000/business')
      .then(response => {
        this.setState({ business: response.data });
      })
      .catch(function (error) {
        console.log(error);
      })
}
tabRow(){
  return this.state.business.map(function(object, i){
      return <TableRow obj={object} getBusinessData={this.getBusinessData} key={i} />;
  });
}

And in TableRow:

delete() {
    axios.get('http://localhost:4000/business/delete/'+this.props.obj._id)
        .then(() => {
            console.log('Deleted'));
            this.props.getBusinessData();
        })
        .catch(err => console.log(err))
}

Note that you have to pass the method down to the TableRow as a prop as well so that it can access it.

Upvotes: 1

Related Questions