【发布时间】:2019-02-19 06:34:27
【问题描述】:
我的状态中有 momentjs 对象
state = {
startDate: getNow() //this funtion return a momemtJS object
}
在一个函数中,我需要获取 startDate 前一年的日期
const dateBeforeOneYear = this.state.startDate.subtract(1, 'years');
但如果我这样做,我会错误地修改状态
所以我尝试复制状态
const copyStartDate = {...this.state.startDate}
const copyStartDate = this.state.startDate.subtract(1, 'years');
但现在我得到了错误,substract 不是一个函数,我猜是因为 copyStartDate 不再是 MomemntJs
【问题讨论】:
-
仅将日期存储在状态变量中,只要您希望时刻对象将日期传递给时刻,我认为这将是一种更好的方法。