【问题标题】:React Date-Picker won't accept a DateReact Date-Picker 不接受日期
【发布时间】:2019-07-25 02:50:51
【问题描述】:

我正在尝试在我的应用程序中实现这个日期选择器: https://www.npmjs.com/package/react-date-picker

我已经像这样初始化了组件:

        <Grid item xs={7}>
            <DatePicker
                name="Date"
                value={this.getDate}
                onChange={this.handleDateChange}
            />
        </Grid>

value 函数返回日期,如下所示:

  getDate = () => {
    const dateToday = Service.todaysDate //service returns a string so we convert
      return new Date(dateToday)
}

但是我得到了这个没有任何意义的打字稿错误!!

Type '() => Date' is not assignable to type 'Date | Date[] | undefined'.   Type '() => Date' is missing the following properties from type 'Date[]': pop,

我是新来的反应,我很难弄清楚这一切。任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: reactjs typescript datepicker material-ui


    【解决方案1】:

    您应该将值与实际的 js 变量绑定,这不起作用。 此外,事件应该绑定到 lambda 表达式。 所以在你的 render() 函数中你应该添加

    let date = this.getDate();
    return  (<Grid item xs={7}>
            <DatePicker
                name="Date"
                value={date}
                onChange={(event) => this.handleDateChange(event)}
            />
        </Grid>);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多