从React js中的列表中删除项目

时间:2017-05-10 13:10:46

标签: reactjs

我正在创建一个待办事项应用程序&单击它们时,我无法编写用于删除列表元素的代码。我想要在用户点击它时删除特定项目

class Todo extends React.Component {

    constructor(props) {
      super(props);
      this.state={todos:[]};
    }

    save() {
      var todos = [...this.state.todos];
      todos.push(this.newText.value);
      this.setState({todos});
    }

    remove{

    }


    render(){
        return(
            <div className="list">
              <h1> TO-DO List</h1>
              <input type="text" ref={(ip) => {this.newText = ip}}/>
              <button onClick={this.save.bind(this)} className="btn btn-primary glyphicon glyphicon-floppy-saved">Save
              </button>
              <ul>
                {this.state.todos.map(function(todo) {
                      return <li>{todo}</li>

                 })}

              </ul>
            </div>
        )
    }
};

2 个答案:

答案 0 :(得分:2)

你需要传递todo的索引,然后使用javascript中的切片函数删除它,如

remove(e, index){
      var todos = [...this.state.todos];
      todos.slice(index, 1);
      this.setState({todos})
}

class Todo extends React.Component {

    constructor(props) {
      super(props);
      this.state={todos:[]};
    }

    save() {
      var todos = [...this.state.todos];
      todos.push(this.newText.value);
      this.setState({todos});
    }

    deleteTodo(index){
        console.log(index)
         var todos = [...this.state.todos];
         todos.splice(index, 1)
         this.setState({todos})
    }


    render(){
        return(
            <div className="list">
              <h1> TO-DO List</h1>
              <input type="text" ref={(ip) => {this.newText = ip}}/>
              <button onClick={this.save.bind(this)} className="btn btn-primary glyphicon glyphicon-floppy-saved">Save
              </button>
              <ul>
                {this.state.todos.map(function(todo, index) {
                      return <li key={index} onClick={this.deleteTodo.bind(this, index)}>{todo}</li>

                 }.bind(this))}

              </ul>
            </div>
        )
    }
};

ReactDOM.render(<Todo/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

答案 1 :(得分:1)

为每个待办事项元素定义onClick方法,绑定名称,如下所示:

{this.state.todos.map((todo) => { //use arrow function to bind the context
       return <li onClick={this._deleteTodo.bind(this, todo)}>{todo}</li>
})}

每当您点击任何待办事项时,它都会将其名称传递给onClick函数,现在使用indexOf来计算array中该项的索引,并使用{{ 3}}从列表中删除它,例如ethis:

_deleteTodo(value){
    let todos = this.state.todos.slice();  //create a copy of that array first
    todos.splice(todos.indexOf(value), 1);
    this.setState({todos});  
}

检查工作示例:

&#13;
&#13;
class Todo extends React.Component {

    constructor(props) {
      super(props);
      this.state={todos:[]};
    }

    save() {
      var todos = [...this.state.todos];
      todos.push(this.newText.value);
      this.setState({todos});
    }

    _deleteTodo(value){
       let todos = this.state.todos.slice();  
       todos.splice(todos.indexOf(value), 1);
       this.setState({todos});  
    }


    render(){
        return(
            <div className="list">
              <h1> TO-DO List</h1>
              <input type="text" ref={(ip) => {this.newText = ip}}/>
              <button onClick={this.save.bind(this)} className="btn btn-primary glyphicon glyphicon-floppy-saved">Save
              </button>
              <ul>
                {this.state.todos.map((todo) => {
                    return <li onClick={this._deleteTodo.bind(this, todo)}>{todo}</li>
                })}
              </ul>
            </div>
        )
    }
};

ReactDOM.render(<Todo/>, document.getElementById('app'))
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>
&#13;
&#13;
&#13;