0

使用 react-datepicker 并传入日期时,我无法编辑日期。

  _updateStartDate = (value) => {

    this.setState({ startDate: value });
  }



<DatePicker 
    selected={startDate ? moment(startDate, 'DD-MM-YYYY') : moment()}
    onSelect={(value) => {this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }} 
    onChange={(value) => { this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }}
 />
4

2 回答 2

1

startDate 未定义。如果你传递这个,你可能想尝试这样的事情。

_renderEffectiveStartDateCell = (startDate) => {
    return (<DatePicker 
              selected={startDate ? moment(startDate, 'DD-MM-YYYY') : moment()}
              onSelect={(value) => {this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }} 
              onChange={(value) => {this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }}
          />);  } 

这将允许日期选择器使用传入的 startDate 或者只是当前日期没有传入。

如果不将此值作为参数传递,您将获得未定义的 startDate。

于 2018-09-25T18:00:26.597 回答
0

看起来你有一个轻微的错字

你的意思是参考this.state.startDate吗?

<DatePicker 
    selected={this.state.startDate ? moment(this.state.startDate, 'DD-MM-YYYY') : moment()}
    onSelect={(value) => {this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }} 
    onChange={(value) => { this._updateStartDate(moment(value).format('YYYY-MM-DD HH:mm:ss')) }}
 />
于 2018-09-25T17:19:31.050 回答