Hi i tried to make a MERN app
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
// indexponent.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: []};
}
ponentDidMount(){
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.
Hi i tried to make a MERN app
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.ponent.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: []};
}
ponentDidMount(){
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.
Share Improve this question asked Jan 3, 2019 at 14:37 Saif EjjilaliSaif Ejjilali 1072 gold badges3 silver badges12 bronze badges3 Answers
Reset to default 1It looks like you only get
the business data from the API in the ponentDidMount
of the Index
Component.
Instead, you should have a method that gets the data from the API and call it both in the ponentDidMount
and in the .then
of the delete
method in TableRow.
For example:
ponentDidMount(){
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.
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
You must update your data in the callback of your delete/edit request.
Currently, you just log that the deletion was successful.