使用带有redux-form的react-date会导致错误

时间:2018-03-16 15:32:48

标签: javascript reactjs redux-form react-dates

我正在尝试将react-datesredux-form一起使用。为render做了一件事。我处理text inputselect字段的方式几乎相同。那些工作正常。 在DateRangePicker甚至SingleDatePicker上获得一个时髦的错误,这是我无法理解的。任何想法/建议都非常感谢。

渲染组件是否为:

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

我的课程只是一个表格:

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)

由于某种原因,DateRangePicker会导致一个奇怪的错误:Uncaught TypeError: Cannot read property 'createLTR' of undefined

我错过了什么?

2 个答案:

答案 0 :(得分:0)

我认为该错误是由于缺少对React-dates的初始化导入而引起的,您可以查看(https://github.com/airbnb/react-dates)中的Initialize部分

答案 1 :(得分:0)

import 'react-dates/initialize';

看起来好像还有DateRangePicker的更新:

因此,将starteDateId和endDateId包含在DateRangePicker组件中。

<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}
    />

对我有用。