Search code examples
reactjsreact-datepicker

react-datepicker not editable when passing in selected date


When using react-datepicker and passing in a date I am not able to edit the date.

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

Solution

  • The startDate is not defined. If you are passing this in you might want to try something like this.

    _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')) }}
              />);  } 
    

    This will allow the datepicker to use the startDate passed in or just current date is nothing is passed in.

    Without passing in this value as an argument you are going to get undefined for startDate.