我们应该使用第一种方式还是第二种方式来更新状态中的设置对象?哪个是可变的最佳实践?不变的概念?state = { settings: { at: [], // other keys }}addHour = () => { const { settings } = this.state; const time = moment(); time.set({ minutes: 0, seconds: 0 }); // First Way settings.at.push(time); this.setState({ settings }); // Second Way const new_settings = { ...settings at: [...settings.at, time] } this.setState({ settings: new_settings });};
添加回答
举报
0/150
提交
取消