admin管理员组文章数量:1355675
I'm trying to prepend an item to a list:
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)});
}
But it ALWAYS renders LAST!
<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>
I really don't know why if I'm adding it to the beginning of the list it is rendering last...
I'm trying to prepend an item to a list:
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)});
}
But it ALWAYS renders LAST!
<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>
I really don't know why if I'm adding it to the beginning of the list it is rendering last...
Share Improve this question asked Sep 3, 2018 at 1:41 danielrvtdanielrvt 10.9k21 gold badges84 silver badges129 bronze badges 2-
2
people: {[newPerson].concat(this.state.people)}
You've got that wrapped in curly brackets. Is that in the actual code, or a typo when posting? Also, it looks like all new persons will get an identical id; is that intentional? – Nicholas Tower Commented Sep 3, 2018 at 1:44 -
use unique value for
key
and all should be fine – marzelin Commented Sep 3, 2018 at 1:57
4 Answers
Reset to default 4You can use a spread on original array and remove the {}
wrapping new array
this.setState({addingPerson: true, people: [newPerson, ...this.state.people]);
Consider the unshift()
method, which you can use to prepend one or more elements to the people
array.
Keep in mind that unshift()
method mutates the array that it's called on:
addPersonHandler = () => {
const newPerson = {
id: "new",
edit: true,
name: "",
dni: "",
onDelete: this.discardHandler
};
// Get people array from current state
const people = this.state.people;
// Prepends the newPerson to the start of people array
people.unshift(newPerson);
// Return the updated state to your ponent for re-render
this.setState({ addingPerson : true, people : people });
}
Try like this, take state in one new array variable and push new object into it, finally update the state.
addPersonHandler = () => {
const newPerson = {
id: "new",
edit: true,
name: "",
dni: "",
onDelete: this.discardHandler
};
let pplArr = this.state.people;
pplArr.push(newPerson);
this.setState({ addingPerson: true, people: pplArr });
};
<ul>
<Person
id={p.id}
name={p.name}
dni={p.dni}
onDelete={p.id=="new"? this.discardHandler: this.deleteHandler}
edit={p.edit}
/>
People.map((p, i) => {
return <li key={p.id}>
</li>
});
</ul>
本文标签: javascriptReact prepend item to listStack Overflow
版权声明:本文标题:javascript - React prepend item to list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744045210a2581395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论