A
A
Alexey Milyutin2019-07-02 22:07:20
JavaScript
Alexey Milyutin, 2019-07-02 22:07:20

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)]
    })
  }

child component:
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

1 answer(s)
V
Vladimir Proskurin, 2019-07-02
@doppelgangerz

Why are there curly braces?

this.setState({
      text: [this.state.text.splice(index, 1)]
    })

this way you will get an array that will have one element - the array (from which you removed the element).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question