如何将JavaScript输出添加到React Native中的初始状态



想要初始化日期为

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添加到没有keystate object

我假设您要将日期设置为当前日期,因为您将其放入卷曲括号中,以便期望对象。只需卸下卷曲牙套即可。

最新更新