Skip to content Skip to sidebar Skip to footer

How Can I Delete A Row On Fetch By Onclick In Reactjs

I am very new to this i dont know how to delete a row by using fetch.. ive already made a messed up code i don't know how it will work now please help me im so lost... renderItem

Solution 1:

After deleting your element from api you also need to remove it from your state, Suppose you are rendering your table from state employee. then you need to do

deleteEmployee(id) {
    debugger
      fetch ('http://localhost:5118/api/employeedetails/deleteemployeedetail/'+ id,
      { method: 'DELETE',})
      .then( 
          res =>  {
              this.setState({jsonReturnedValue : json})
              var employee = [...this.state.employee];
              var idx = employee.findIndex(item => item.Employee_ID === id);
              employee.splice(idx, 1);
              this.setState({employee})

         }
      )
      .catch( err => cosole.error(err))                
  }  

Post a Comment for "How Can I Delete A Row On Fetch By Onclick In Reactjs"