我很确定代码是正确的,但是当我按下删除按钮时,我仍然收到错误消息“ TypeError:this.setState.myItems is undefined”。我尝试在filter()方法中使用setState删除特定项目。在第35行:constfilteredItems = this.setState.myItems.filter(myItem => {在那边 import React from "react"; import PropTypes from "prop-types"; import { returnStatement, isTemplateElement } from "@babel/types"; import "./style.css"; class App extends React.Component { constructor(props) { super(props); this.state = { myItems: ["Poco F1", "OnePlus 6", "MiA1", "S7 Edge"] }; } addItem(e) {e.preventDefault();const { myItems } = this.state;const newItem = this.newItem.value;const exists = myItems.includes(newItem);if (exists) { this.setState({ message: "This Phone is already listed" });} else { newItem !== "" && this.setState({ myItems: [...this.state.myItems, newItem] });}this.newForm.reset(); } removeItem(item) {const filteredItems = this.setState.myItems.filter(myItem => { return myItem !== item;}); this.setState({ myItems: [...filteredItems] }); } render() { const { myItems, message } = this.state; return ( <div> <h1> <b>PHONE LIST</b> </h1> <form ref={input => (this.newForm = input)} onSubmit={e => this.addItem(e)} > <div className="form-group"> <label className="sr-only" htmlFor="newItemInput"> {" "} ADD NEW PHONE </label> <input type="text" ref={input => (this.newItem = input)} placeholder="Enter Phone Name" className="form-control" id="newItemInput" /> </div> <button type="submit">ADD</button>我想在filter()方法中使用setState删除特定项
添加回答
举报
0/150
提交
取消