Remove item from array in React

后端 未结 8 819
醉酒成梦
醉酒成梦 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(
            

      My Todo List

      Add item

      this.textChange(e)}/>
        {this.state.list.map((x,y) => { return
      • {x}
      • })}
      ) } } export default Todo;

    提交回复
    热议问题