想要初始化日期为
constructor(){
this.state={
date:{new Date().getDate()+"-"+new Date().getMonth()+"-"+new Date().getFullYear()}
}
}
但不起作用。我们如何将JS添加到状态?
您需要删除卷曲括号以实现此
date:new Date().getDate()+"-"+new Date().getMonth()+"-"+new Date().getFullYear()
现在,您正在将object
添加到没有key
的state object
我假设您要将日期设置为当前日期,因为您将其放入卷曲括号中,以便期望对象。只需卸下卷曲牙套即可。