Remove item from array in React

后端 未结 8 815
醉酒成梦
醉酒成梦 2021-01-03 12:45

I have problem with removeItem function (it should remove current

  • that button is nested in, and item from array on this.state.list), no code currentl
  • 相关标签:
    8条回答
    • 2021-01-03 13:17

      import React, { Component } from 'react';
      import './Todo.css';
      
      class Todo extends Component {
        constructor(props) {
          super(props);
          this.state = {
            list: [],
            text: ''
          }
          this.textChange = this.textChange.bind(this);
          this.addToList = this.addToList.bind(this);
        }
      
        textChange(e) {
          this.setState({
            text: e.target.value
          })
        }
      
        addToList() {
          this.setState(prevState => ({
            list: prevState.list.concat(this.state.text),
            text: ''
          }))
        }
      
        removeItem(index) {
          let newList = this.state.list.splice(index,1);
          this.setState({list:newList})
        }
      
        render() {
          return(
            <div>
              <h1>My Todo List</h1>
              <h3>Add item</h3>
              <input value={this.state.text} onChange={e => this.textChange(e)}/>
              <button onClick={this.addToList}>+</button>
              <ul>{this.state.list.map((x,y) => {
                return <li key={y}>{x}
                <button onClick={this.removeItem.bind(this,y)}>-</button>
                </li>})}
              </ul>
            </div>
          )
        }
      }
      
      export default Todo;

      0 讨论(0)
    • 2021-01-03 13:18

      I think you should pass the index of the item to your removeItem function. Like so:

      removeItem(index) {
        const list = this.state.list;
      
        list.splice(index, 1);
        this.setState({ list });
      }
      
      render() {
        return(
          <div>
            <h1>My Todo List</h1>
            <h3>Add item</h3>
            <input value={this.state.text} onChange={e => this.textChange(e)}/>
            <button onClick={this.addToList}>+</button>
            <ul>{
              this.state.list.map((text, i) => {
                return (
                  <li key={i}>
                    {text}
                    <button onClick={() => this.removeItem(i) }>-</button>
                  </li>
                );
              })}
            </ul>
          </div>
        )
      }
      
      0 讨论(0)
    • 2021-01-03 13:19

      You can filter your list by the issue you want, and it will be auto removed, for example, if you want to remove all items = 3 :

      list: prevState.list.filter(x=> x != 3);

      Good luck!

      0 讨论(0)
    • 2021-01-03 13:25

      Removing item from array by index:

      const newList = this.state.list.splice(index, 1);
      

      Removing item from array by value:

      const newList = this.state.list.splice(this.state.list.indexOf(value), 1);
      
      0 讨论(0)
    • 2021-01-03 13:30

      in my solution eg:

      const remove = (i) => {
              const arr = data.filter((item) => item.name !== i);
              setData(arr);
          };
      

      I filtered the items that are not removed and set again the state

      0 讨论(0)
    • 2021-01-03 13:34

      I would pass the index of the item in the list on click then splice the array:

      <ul>
        {
          this.state.list.map((x,y) => {
            return (
              <li key={y}>
                {x}
                <button onClick={() => this.removeItem(y)}>-</button>
              </li>
            );
          })
        }
      </ul>
      

      Then in removeItem:

      removeItem(index) {
        const list = this.state.list;
        list.splice(index, 1);
        this.setState({ list });
      }
      
      0 讨论(0)
    提交回复
    热议问题