【发布时间】:2018-05-16 14:23:21
【问题描述】:
我正在尝试将状态中的时刻对象格式化为字符串,然后以不可变的方式发送到数据库,以免改变状态。
this.state = {
startDate: moment()
}
状态需要是 React DatePicker 的对象。 所以在提交我的表单时,我想将 startDate 更改为字符串。
handleSubmit(event) {
event.preventDefault();
const newState = {
...this.state,
startDate : { ...this.state.startDate }
};
newState.startDate.format('YYYY-MM-DD');
this.RegisterEmployeeService.registerEmployee(newState)
.then((res) => {
Swal('Registered!');
})
}
但在 newState.startDate 对象上运行“.format()”时出现错误。
<DatePicker
selected={this.state.startDate}
onChange={this.handleStartDateChange.bind(this)}/>
错误:
TypeError: newState.startDate.format is not a function
【问题讨论】:
-
错误是什么?
-
道歉,我把它放在上面了
标签: reactjs ecmascript-6 redux datepicker momentjs