【问题标题】:react-datepicker with no initial valuereact-datepicker 没有初始值
【发布时间】:2015-03-11 14:02:10
【问题描述】:

我开始使用 react-datepicker 组件。 https://github.com/Hacker0x01/react-datepicker

我运行了示例,现在我想对其进行调整,使其没有初始值。 示例代码如下:

 <script type="text/javascript">
 var container = document.querySelector('#datepicker-container');
 var exampleComponent = React.createClass({
    displayName: 'exampleComponent',
    getInitialState: function() {
      return {
         start_date: moment(),
      };
    },
    handleStartDateChange: function(date) {
       this.setState({
          start_date: date
       });
    },
   render: function() {
      return React.DOM.div({}, [
         DatePicker({
         key: 'example1',
         selected: this.state.start_date,
         onChange: this.handleStartDateChange
      }),

      ]);
   }
})
React.renderComponent(exampleComponent(), container);
</script>

我尝试使用 selected: none 甚至不使用 selected,但随后出现以下错误:

 TypeError: newProps.date is null
     value: newProps.date.format(this.props.dateFormat)

我也查看了源代码,但没有发现任何以空日期开头的可能性。 提前感谢您的帮助!

【问题讨论】:

    标签: datepicker reactjs


    【解决方案1】:

    你应该可以通过在你的getInitialState中设置start_date: null来实现你想要的,

    或者,如果您使用 react-datepicker 中的示例 3,您可以定义如下函数:

    handleResetDate: function() {
        this.setState({
            new_date: null
        });
    },
    

    ,并在您想要重置日期时调用它。
    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2021-10-26
      • 1970-01-01
      • 2019-04-20
      • 2012-04-12
      相关资源
      最近更新 更多