After delete it doesn't remove the row from the table. After on-click action, it is updating the Database, Array, and the state correctly, but doesn't render my view, because I still see the row. Once I leave the page and come back, the record is gone; However, that makes sense because the database is updated by post request. I have consoled the array as well as the state, both are updating after delete.
import React,{ PureComponent } from 'react';
import ReactDom from 'react-dom';
import axios from 'axios';
class Listing extends PureComponent{
state = {
categories: []
};
componentDidMount(){
axios.get('http://127.0.0.1:8000/category')
.then(response=>{
this.setState({categories:response.data});
});
}
deleteCategory = (e)=>{
axios.delete('http://127.0.0.1:8000/category/delete/'+e)
.then(response=> {
var array = this.state.categories;
for(var i = 0 ; i < array.length ; i++){
if(array[i].id == e){
array.splice(i,1);
this.setState({categories:array});
}
}
});
}
render() {
return(
<div>
<table className="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Category Name</th>
<th scope="col">Status</th>
<th scope="col">Created At</th>
<th scope="col">Updated At</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{
this.state.categories.map(category=>{
return(
<tr>
<th scope="row">{category.id}</th>
<td>{category.name}</td>
<td>{category.active == 1 ? ("Active"): ("InActives")}</td>
<td>{category.created_at}</td>
<td>{category.updated_at}</td>
<td><button className="btn btn-danger" onClick={(e)=>this.deleteCategory(category.id)}>Delete</button> </td>
</tr>
)
})
}
</tbody>
</table>
</div>
);
}
}
export default Listing;