加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-datepicker没有初始值

发布时间:2020-12-15 20:14:36 所属栏目:百科 来源:网络整理
导读:我开始使用react-datepicker组件. https://github.com/Hacker0x01/react-datepicker 我让示例运行,现在我想调整它没有初始值. 示例代码如下: script type="text/javascript" var container = document.querySelector('#datepicker-container'); var example
我开始使用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或甚至选择退出,但后来我收到以下错误:

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

我也查看了源代码,但没有发现任何可能以空日期开始.
在此先感谢您的帮助!

解决方法

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

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

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

,并在需要重置日期时调用它.希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读