简体   繁体   English

如何修复 react-dates SingleDatePicker 无效日期?

[英]How to fix react-dates SingleDatePicker invalid date?

I'm attempting to use SingleDatePicker to edit/add events in a database.我正在尝试使用 SingleDatePicker 在数据库中编辑/添加事件。 I can create a new event outside of this calendar year, but when I go to edit any event past 2019, I am getting an 'invalid date' in my SingleDatePicker which crashes my site when clicked and gives the error "Uncaught TypeError: month must be a valid moment object".我可以在这个日历年之外创建一个新事件,但是当我去编辑 2019 年以后的任何事件时,我在我的 SingleDatePicker 中收到一个“无效日期”,单击它时我的网站崩溃并给出错误“Uncaught TypeError: month must成为一个有效的矩对象”。 I am using momentjs.我正在使用 momentjs。 It's worth noting that all dates are passed from my API in the exact same format, and I only have this issue with dates with years after 2019. When a date is after 2019 i get the error "Warning: Failed prop type: Invalid prop date of type object supplied to SingleDatePicker , expected Moment . " I am using moment though so i do not understand this error.值得注意的是,所有日期都是从我的 API 以完全相同的格式传递的,我只有 2019 年之后的日期才有这个问题。当日期在 2019 年之后时,我收到错误“警告:失败的道具类型:无效的道具date提供给SingleDatePicker的类型object ,预期为Moment 。“不过我正在使用 moment,所以我不明白这个错误。

I've found no documentation of this issue.我没有找到有关此问题的文档。

`componentWillReceiveProps() {
if (this.props.date !== null) {
  let newDate = moment(this.props.date, "ddd, MMM DD yyyy HH:mm:ss 
Z");
  this.setState({ date: newDate });
}
....

....
....
....

<SingleDatePicker
                date={this.state.date} 
                isOutsideRange={() => false}
                onDateChange={date => this.setState({ date })} /
                focused={this.state.focused} 
                onFocusChange={({ focused }) => this.setState({ 
                focused })} 
                id="selectedDate" ,
              />`

I did a workaround that solved for me.我做了一个为我解决的解决方法。 When the date is null (like on our case) you can set it to today's date with moment like this:当日期为空时(就像我们的例子一样),您可以将它设置为今天的日期,如下所示:

onDateChange={date => this.setState({ date || moment().format('YYYY-MM-DD') })}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM