【问题标题】:react-datepicker not editable when passing in selected date传入选定日期时,react-datepicker 不可编辑
【发布时间】:2018-09-25 17:17:14
【问题描述】:

使用 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')) }}
 />

【问题讨论】:

  • 你有没有从你的状态对象中解构startDate?由于您的三元运算符,它将始终诉诸moment(),因为startDate 将是undefined

标签: reactjs react-datepicker


【解决方案1】:

开始日期未定义。如果你要传递这个,你可能想尝试这样的事情。

_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。

【讨论】:

    【解决方案2】:

    你好像有一个小错误

    您的意思是引用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')) }}
     />
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2020-04-03
      • 2010-10-11
      • 2013-06-10
      • 1970-01-01
      相关资源
      最近更新 更多