【发布时间】:2018-09-03 01:41:12
【问题描述】:
我正在尝试将一个项目添加到列表中:
addPersonHandler = () => {
const newPerson = {
id: "new",
edit: true,
name: "",
dni: "",
onDelete: this.discardHandler
};
// I want to prepend the new person to the people list.
this.setState({addingPerson: true, people: {[newPerson].concat(this.state.people)});
}
但它总是最后渲染!
<ul>
People.map((p, i) => {
return <li key={p.id}>
<Person
id={p.id}
name={p.name}
dni={p.dni}
onDelete={p.id=="new"? this.discardHandler: this.deleteHandler}
edit={p.edit}
/>
</li>
});
</ul>
我真的不知道为什么如果我将它添加到列表的开头它会在最后呈现......
【问题讨论】:
-
people: {[newPerson].concat(this.state.people)}你已经用大括号括起来了。是在实际代码中,还是在发布时出现拼写错误?此外,看起来所有新人都会获得相同的 id;这是故意的吗? -
为
key使用唯一值,一切都会好起来的
标签: javascript reactjs prepend