Answer the question
In order to leave comments, you need to log in
What is the correct way to remove an element from an array?
Hello, I have a similar implementation, but it does not work correctly (a non-specific element in the list is deleted):
render() {
return (
<div className="container">
<div className="app">
<div className="top-container">
<div className="name">Todo List</div>
<InputArea changeState={this.changeState}/>
<input className="clear_button" type="button" value="Clear" onClick={this.handleClear}/>
</div>
<TodoList closed={this.handleClose} text={this.state.text}/>
</div>
</div>
)
}
handleClose = (index) => {
console.log(index);
this.setState({
text: [this.state.text.splice(index, 1)]
})
}
render() {
return (
<div>
<ul className="todoList_container">
{this.props.text.map((text, i) => {
return <div key={i} className="li" >
<li>{text}</li>
<i onClick={()=> {this.props.closed(i)}} className="fas fa-times-circle close-btn"></i>
</div>
})}
</ul>
</div>
)
}
Answer the question
In order to leave comments, you need to log in
Why are there curly braces?
this.setState({
text: [this.state.text.splice(index, 1)]
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question