我想要在列表中添加一个元素:
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>
我真的不知道为什么如果我把它添加到列表的开头,它会最后渲染出来...
这句话与IT技术有关。
people: {[newPerson].concat(this.state.people)}
這裡用了大括號,請問是原始碼本身就有還是在發布時打錯了呢?此外,看起來所有的新人都會得到相同的ID,這樣是故意的嗎? - Nicholas Towerkey
,所有的东西都会正常。 - marzelin