Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  87] [ 4]  / answers: 1 / hits: 11615  / 3 Years ago, fri, march 5, 2021, 12:00:00

My problem is when I am clicking on the delete button , I can delete the item from table, but I need to refresh page manually. How can I solve to refreshing it automatically?
Could anyone solve my problem?
I also attached my code section below.
Here is my code section, also Home components and RowCreator component as well:


import React from 'react'
import axios from 'axios'
import {Link} from 'react-router-dom'
import '../App.css';
class Home extends React.Component{
constructor(props) {
super(props)

this.state = {
metersData:[]


}


}



componentWillMount(){
axios.get('http://localhost:8080/meterservices/api/meters').then(res=>{
const metersData = res.data;
this.setState({metersData})

})
}




render(){
return (<div>
<h2>Measuring instruments / Meetinstrumenten:</h2>
<table align='center'>
<thead>
<tr>
<th>Id</th>
<th>Function of the meters</th>
<th>Profile</th>


</tr>
</thead>
<tbody>
{this.state.metersData.map(meter =><RowCreator item={meter} />)}


</tbody>

</table>
<br/>
<Link to={'/addMeter'}><font size="5"><span>Register new Meter device</span></font></Link>


</div>)
}
}


class RowCreator extends React.Component{

handleClick = (event) => {
event.preventDefault();
var meter = this.props.item;

console.log(meter.id)
axios.delete('http://localhost:8080/meterservices/api/meters/'+meter.id).then(res=>{


const metersData = res.data;
this.setState({metersData})

})
}

render(){
var meter = this.props.item;
return<tr>
<td>{meter.id}</td>
<td>{meter.name}</td>
<td>{meter.profile_name}</td>

<td ><Link to={'/meterDetails/'+meter.id}><span>Add monthly data</span></Link></td>
<td><Link to={'/analyze/'+meter.id}><span>Analyze monthly data</span></Link></td>
<button onClick={this.handleClick} >Delete </button>

</tr>
}
}

export default Home;

More From » reactjs

 Answers
4

I would suggest moving your delete call up to a function in Home that takes an id, and passing that function as a prop to RowCreator.


The state inside the RowCreator is never used and changing it doesn't really help you. What you want is to tell Home that there's been a change and it needs to update its state.


[#1692] Saturday, February 27, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
teagan

Total Points: 98
Total Questions: 106
Total Answers: 101

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
teagan questions
Mon, Feb 15, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
;