简体   繁体   中英

How to use react-dates with redux-form

I am working on react-dates with redux-form . Logic is already implemented but when I checked the console for it values it showing nothing. I've already tried but could not able to resolve this issue. Could someone please help me how to resolve this issue. Thanks

<Field component={DateRanger} type="text" name="date" label="Select Project" />

Date Component

 export class DateRanger extends Component {
  constructor(props) {
    super(props)
    this.state = {
      startDate: null,
      endDate: null,
      focusedInput: null,
    }
  }

  onFocusChange = (value) => {
    this.setState({ focused: !this.state.focused })
    const { input } = this.props
    input.onFocus(value)
  }

  render() {
    const { startDate, endDate, focusedInput } = this.state
    const { input } = this.props
    return (
      <div className="defaultBorderColor">
        <DateRangePicker
          {...input}
          isOutsideRange={(day) => !isInclusivelyBeforeDay(day, moment())}
          startDate={startDate}
          className="activeDatePicker"
          date={input.value}
          endDate={endDate}
          startDateId="date_input_start"
          endDateId="date_input_end"
          onDatesChange={({ startDate: dateStart, endDate: dateEnd }) => {
            input.onChange(input.value)
            this.setState(() => {
              return {
                startDate: dateStart,
                endDate: dateEnd,
              }
            })
          }}
          numberOfMonth={1}
          focusedInput={focusedInput}
          onFocusChange={(input) => this.setState({ focusedInput: input })}
          showClearDates
          minimumNights={0}
        />
      </div>
    )
  }
}

You are sending for input.onChange(input.value) the same previous value that redux-form holds. so you are not changing the form state actually.

      onDatesChange={({ startDate: dateStart, endDate: dateEnd }) => {
        input.onChange(input.value)
        this.setState(() => {
          return {
            startDate: dateStart,
            endDate: dateEnd,
          }
        })
      }}

You need to change it to

      input.onChange({
        startDate: dateStart,
        endDate: dateEnd,
      })

i would also point that you dont need, IMO, to both hold an internal state and redux state. this should be a controlled input by redux form. you get input.value and input.onChange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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