简体   繁体   English

任何人都可以告诉我为什么我无法删除待办事项列表中的项目?

[英]can anyone tell me why i am unable to delete an item from the todo list?

Im having issues with react todo list. 我有反应todo列表的问题。

When submitted the list item appears fine. 提交时,列表项似乎没问题。

I should then be able to delete this be clicking on the item,however nothing happens. 然后我应该能够删除这个点击该项目,但没有任何反应。 As soon as i try to add another item the pages refreshes and all items are removed? 一旦我尝试添加另一个项目,页面刷新并删除所有项目?

console log just shows 控制台日志只显示

[object object] [对象]


App 应用

import React, { Component } from 'react';
import './App.css';
import TodoItem from './TodoItem';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: []
    };
    this.addItems = this.addItems.bind(this);
    this.deleteItem = this.deleteItem.bind(this);
  }

  addItems(e) {
    if (this._inputElement !== '') {
      let newItem = {
        text: this._inputElement.value,
        key: Date.now()
      };
      this.setState(prevState => {
        return {
          items: prevState.items.concat(newItem)
        };
      });
    }
    this._inputElement.value = '';
    e.preventDefault();
  }

  deleteItem(key) {
    console.log('key is' + key);
    console.log('itesm as' + this.state.items);
    var filteredItems = this.state.items.filter(function(item) {
      return item.key !== key;
    });
    this.setState = {
      items: filteredItems
    };
  }

  render() {
    return (
      <div className="app">
        <h2> things to do</h2>
        <div className="form-inline">
          <div className="header">
            <form onSubmit={this.addItems}>
              <input
                ref={a => (this._inputElement = a)}
                placeholder="enter task"
              />
              <button type="submit">add</button>
            </form>
          </div>
        </div>
        <TodoItem entries={this.state.items} delete={this.deleteItem} />
      </div>
    );
  }
}
export default App;

todoItem 的TodoItem

import React, { Component } from 'react';

//search bar
class TodoItem extends Component {
  constructor(props) {
    super(props);
    this.createTasks = this.createTasks.bind(this);
  }
  createTasks(item) {
    return (
      <li onClick={() => this.delete(item.key)} key={item.key}>
        {item.text}
      </li>
    );
  }
  delete(key) {
    console.log('key is ' + key);
    this.props.delete(key);
  }
  render() {
    let todoEntries = this.props.entries;
    let listItems = todoEntries.map(this.createTasks);
    return <ul className="theList">{listItems}</ul>;
  }
}

export default TodoItem;

You are assigning to setState instead of using it as a method that it is 您正在分配给setState而不是将其用作方法

Change 更改

this.setState = {
  items: filteredItems
};

to

this.setState({
  items: filteredItems
});

And that is also the reason it will reload the app, as you have overwritten the setState method you should be getting an error that setState is not a function and it would crash the app. 这也是它重新加载应用程序的原因,因为你已经覆盖了setState方法,你应该得到一个错误,即setState不是一个函数,它会使应用程序崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从待办事项列表中删除自定义待办事项列表项 - can't delete custom todo list item from todo list 谁能告诉我为什么我收到Uncaught SyntaxError:意外的字符串? - Can anyone tell me why I am getting Uncaught SyntaxError: Unexpected string? 谁能告诉我为什么我收到错误'无法读取未定义的属性'值''? P5.JS - Can anyone tell me why i am getting the error 'Cannot read property 'value' of undefined'? P5.JS 无法从待办事项列表中删除最后一项 - cannot delete last item from todo list 谁能告诉我为什么这一次有效? - Can anyone tell me why this is working once? 谁能告诉我为什么不返回值? - Can anyone tell me why the value is not returned? 谁能告诉我如何以任意角度在JavaScript中的各个项目之间绘制曲线? - Can anyone tell me how to draw curved lines from item to item in javascript for arbitrary angles? 谁能让我知道为什么会收到此错误? - Can anyone let me know why am I getting this error? 我正在尝试为网页创建聊天功能,当我按Enter键时,它会不断重新加载。 谁能告诉我为什么会这样吗? - I am trying to create a chat feature for a web page and it keeps reloading when I hit enter. Can anyone tell me why this might be happening? 我正在尝试使用 Vanilla JavaScript 从 TODO-LIST 应用程序中删除项目 - I am trying to delete Items from in a TODO-LIST App using Vanilla JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM