简体   繁体   中英

Using react-dates with redux-form results in an error

I am trying to use react-dates with redux-form . Did a render thing for it. I have handled text input and select fields pretty much the same way. Those are working fine. Getting a funky error on either DateRangePicker or even SingleDatePicker , which I cannot make sense of. Any ideas/suggestions are greatly appreciated.

Did a render component as:

const renderDateRangePicker = ({
  input,
  focusedInput,
  onFocusChange,
  startDatePlaceholderText,
  endDatePlaceholderText
}) => (
  <DateRangePicker
    onDatesChange={(start, end) => input.onChange(start, end)}
    onFocusChange={onFocusChange}
    startDatePlaceholderText={startDatePlaceholderText}
    endDatePlaceholderText={endDatePlaceholderText}
    focusedInput={focusedInput}
    startDate={(input.value && input.value.startDate) || null}
    startDateId="startDateId"
    endDateId="endDateId"
    endDate={(input.value && input.value.endDate) || null}
    minimumNights={0}
  />
)

My class is just a form as:

class ActivityForm extends Component {
  // values: ['startDate', 'endDate']
  state = {
    focusedInput: null
  }

  onFocusChange(focusedInput) {
    this.setState({ focusedInput });
  }

  render () {
    const { focusedInput } = this.state
    const { handleSubmit, teams } = this.props

    return (
      <form onSubmit={handleSubmit} className="activity__form">
        <div className="activity__form_row">
          <Field
            name="name"
            label="Activity name"
            component={renderTextField}
            margin="normal"
            validate={[required]}
            className="activity__form_field_name"
            InputLabelProps={{
              shrink: true,
            }}
          />
          <div className="activity__form_spacer"/>
          <Field
            name="daterange"
            onFocusChange={this.onFocusChange}
            focusedInput={focusedInput}
            component={renderDateRangePicker}
          />
          <div className="activity__form_spacer"/>
          <Button className="activity__form_button" type="submit">Save</Button>
        </div>
      </form>
    )
  }
}

export default reduxForm({ form: 'activity' })(ActivityForm)

For some reason, DateRangePicker causes a strange error: Uncaught TypeError: Cannot read property 'createLTR' of undefined .

What am I missing?

我认为此错误是由于react-dates初始化的丢失或放错位置引起的,您可以查看( https://github.com/airbnb/react-dates )中的Initialize部分

import 'react-dates/initialize';

It also looks like there is an update to DateRangePicker:

So include starteDateId and endDateId as props to the DateRangePicker component.

<DateRangePicker
      startDateId="2" // PropTypes.string.isRequired,
      endDateId="1" // PropTypes.string.isRequired,
      startDate={this.props.filters.startDate}
      endDate={this.props.filters.endDate}
      onDatesChange={this.onDatesChange}
      focusedInput={this.state.calendarFocused}
      onFocusChange={this.onFocusChange}
      showClearDates={true}
      numberOfMonths={1}
      isOutsideRange={() => false}
    />

It worked for me.

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