【发布时间】:2020-03-29 20:09:34
【问题描述】:
我正在尝试使用新的以下数组更新我的状态,但是即使控制台没有显示任何错误并正确打印出应该形成的新数组,状态也不会更新以反映这一点。
我做错了什么?
state = {
notes: [
{
text: "mow the lawn",
author: "dean",
time: "10am"
},
{
text: "feed the dog",
author: "sam",
time: "2pm"
}
]
};
updateNotes = (title, name, eventTime) => {
let newNote = {
text: title,
author: name,
time: eventTime
};
this.setState(state => {
const list = state.notes.concat(newNote);
console.log(list); //shows the list formed correctly in the console
return {
list
};
});
};
假设我在创建新笔记时输入了值、新文本、一些作者和下午 3 点,运行代码后我在控制台中看到的内容也可供参考
(3) [{…}, {…}, {…}]
0: {text: "mow the lawn", author: "dean", time: "10am"}
1: {text: "feed the dog", author: "sam", time: "2pm"}
2: {text: "new text", author: "some author", time: "3pm"}
【问题讨论】: