【问题标题】:Remove items form the list in React js从 React js 中的列表中删除项目
【发布时间】:2017-05-10 13:10:45
【问题描述】:

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

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

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您需要传递待办事项的索引,然后使用 javascript 中的 slice 函数将其删除,例如

    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>

    【讨论】:

    • remove 函数中有一个错字:而不是slice 你的意思是splice。请解决这个问题。
    【解决方案2】:

    为每个待办事项元素定义一个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 中的索引,并使用splice 从列表,像这样:

    _deleteTodo(value){
        let todos = this.state.todos.slice();  //create a copy of that array first
        todos.splice(todos.indexOf(value), 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(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'))
    <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'/>

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 2016-05-08
      • 2011-03-18
      相关资源
      最近更新 更多