尝试在此组件的状态内将对象添加到数组中,无法将新对象连接到数组而没有“属性未定义”错误。
我已经能够让它在带有数组的“浅”状态下工作,但希望拥有,更愿意让它在内部使用单独的数组。
国家
constructor(props) {
super(props);
this.state = {
show: false,
id: null,
name: 'New Task',
timerOn: false,
seconds: 0,
minutes:0,
hours:0,
complete: {
todayId: null,
list: []
}
}
}
更新状态的方法,制作“控件”(分钟、秒、开始等)的副本,将状态重置为其初始值,获取状态的副本并将其应用于数组:
completeTaskHandler = () => {
if (this.state.seconds === 0 && this.state.minutes === 0 && this.state.hours === 0) {
alert('Please Provide a name');
}
else {
const end = new Date();
let finishedTask = new Object ({
start: this.state.id,
end: end,
name: this.state.name,
seconds: this.state.seconds,
minutes: this.state.minutes,
hours: this.state.hours,});
this.setState((prevState,props) => {
return {
start: null,
show: false,
id: null,
name: 'task',
timerOn: false,
seconds: 0,
minutes:0,
hours:0,
complete: prevState.complete.list.concat(finishedTask)
}
});
clearInterval(this.interval);
}
}