React Datepicker with redux-form

我们两清 提交于 2020-01-13 07:15:34

问题


How to make the react-datepicker bind with redux-form?

I have this redux-form field:

<Field name="due_date" component={props =>
  <DatePicker
    {...props}
    readOnly={true}
    selected={this.state.due_date}
    value={this.state.due_date}
    onChange={this.handleDueDate}  
  />
} />

Whenever i submit the redux form does return an empty object not binding the datepicker's value...

my onChange :

  handleDueDate(date) {
    this.setState({
      due_date: moment(date).format('L')
    }, () => {
      // do I need to dispatch and action here to update the redux-form in state tree? 
    })
  }

回答1:


you have to make a custom component to validate and use datepicker with redux-form

Try this

const datePicker = ({ input, label, type, className, selected, meta: { touched, error } }) => (
      <div>
        <div>
          <DatePicker {...input}
            selected={selected} placeholder={label}
            type={type} className={className}
            peekNextMonth
            showMonthDropdown
            showYearDropdown
            dropdownMode="select"
          />
          {touched && error && <span className="error_field">{error}</span>}
        </div>
      </div>
    )

and then pass props to that custom component

          <Field
            name="date_of_birth"
            component={datePicker}
            type="text"
            selected={this.state.date_of_birth}
            onChange={this.handleChange.bind(this)}
            className="form-control"
          />



回答2:


For future readers, just go to this link: DatePicker in Redux Form

first, I created the component like metioned in link above and just passed the selected prop in it. in my case:

<Field
             name="due_date"
             component={DatePickerComponent}
             selected={this.state.date_of_briefing} />


来源:https://stackoverflow.com/questions/46338367/react-datepicker-with-redux-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!