0

当我在 webpack-dev-server 中呈现它时,日历不会在点击时呈现。

在此处输入图像描述

如果我尝试手动更改日期,则会收到错误消息:

DateSelection.js?f52c:25 Uncaught TypeError: Cannot read property 'calendarFocus' of null

SingleDatePicker 似乎工作得很好,但不是 DateRangePicker。我该怎么做才能在点击时正确呈现日历?

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import moment from 'moment';
import { DateRangePicker } from 'react-dates';
import 'react-dates/initialize';
import 'react-dates/lib/css/_datepicker.css';

class DateSelection extends Component {
  constructor(props) {
    super(props);
    this.state = {
      startDate: moment(),
      endDate: moment(),
      calendarFocus: 'startDate',
    }
    this.onDatesChange = this.onDatesChange.bind(this);
    this.onFocusChange = this.onFocusChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  };

  onDatesChange({ startDate, endDate }) {
    this.setState(() => ({ startDate, endDate }));
  };

  onFocusChange({ calendarFocus }) {
    this.setState(() => ({ calendarFocus }));
  };

  onSubmit(e) {
    e.preventDefault();
    console.log(this.state);
  };

  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <DateRangePicker
          startDate={this.state.startDate}
          startDateId="startDate"
          endDate={this.state.endDate}
          endDateId="endDate"
          onDatesChange={this.onDatesChange}
          focusedInput={this.state.calendarFocus}
          onFocusChange={this.onFocusChange}
        />
        <br/>
        <input type="submit" value="Submit" />
      </form>
    );
  }
};

ReactDOM.render((
  <DateSelection/>
), document.getElementById('root'));

其他详情:

  • 反应 16.2.0
  • 反应日期 16.3.5
  • 网络包 3.10.0
  • 网络包开发服务器 2.11.1

另外,顺便说一句,我似乎找不到当前的 react-dates 文档。很多地方都将我链接到这本故事书,但似乎缺少一些信息。例如,此处的描述列是空白的。

在此处输入图像描述

4

1 回答 1

1

尝试calendarFocusnull构造函数中设置。

和改变

onFocusChange({ calendarFocus }) {
  this.setState(() => ({ calendarFocus }));
};

onFocusChange(calendarFocus) {
  this.setState(() => ({ calendarFocus }));
};
于 2018-02-27T10:05:29.897 回答