我正在尽力从阵列中删除一个项目。
我尝试按以下方式对其进行过滤:
removeContact(contact) {
this.setState({
contacts: this.state.contacts.filter((_, i) => i !== contact)
}); }
但这似乎并不能解决问题。
我的联系人在这样的无序列表中列出:
<ul className="list-group">
{this.state.contacts.map(((contact, index) =>
<li className="list-group-item" key={`${contact.contact}${index}`}>
{contact.name} | {contact.phone} | {contact.countryId} <button onClick={this.removeContact}>Delete</button>
</li>
))
}
</ul>
但是通过单击删除它只会引发错误:
TypeError:无法读取未定义的属性'setState'
用于从api呈现联系人的列表。
答案 0 :(得分:3)
import reprlib
def grep(pattern,lines,sep=")"):
tmp_line=""
for line in lines:
try:
pattern(line.split(sep)[0])
yield tmp_line+"\n"
tmp_line=""
except:
pass
tmp_line+="\n"+line
yield tmp_line
if __name__=="__main__":
lines = ["1) question one:","a) answer 1","b) answer 2","c) answer 3","2) question 2:","a) Not an answer"]
for x in grep(int,lines):
if x is None or len(x) < 3:
continue ## first one will always be None
print(repr(x)) ## one question with answers
print("_______________") ## one question with answers
我想你想要这个。您必须注入索引才能在事件监听器中使用。
removeContact = (index) => () => {
this.setState({
contacts: this.state.contacts.filter((_, i) => i !== index)
});
}