0

Not able to delete my list from the array, I have also used react update addons but it doesn't not work

My delete function

deleteTips(item){
  var array = this.state.newTips;
  var index = array.indexOf(item)
  array.splice(index, 1);
  this.setState({newTips: array });
}

My render function

{ this.state.newTips.map((item, j) => {
  const nameVal = "tips" + j
  return(
      <div style={{width:"8%","float":"right","marginTop":"10px"}}>    
        <img style={{marginLeft: '10px'}} onClick={this.deleteTips.bind(this, item)}/>
      </div>                                                                                
    )
  })
}

1 Answer 1

2

You are modifying the array that is the state of the component, therefor your setState might not work, since the new state is the same as the old state.

Could you try

 deleteTips(item){
   var array = this.state.newTips.filter(function(s) { return s != item });
   this.setState({newTips: array });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.