反应js blueprintjs日期范围选择器不起作用

时间:2018-05-02 15:05:55

标签: javascript reactjs typeerror daterangepicker gettime

我需要在我的组件中的BlueprintJS(documentation)和RangePicker上创建DateRangePicker, 就像在这个screenshot

我安装了所有npm包,并按照说明完成所有操作:

import { DateRangePicker } from "@blueprintjs/datetime";

<DateRangePicker
    value={[this.state.startDate, this.state.endDate]}
    onChange={this.handleDateChange}
/>

但无论如何都有错误:

Cannot read property 'startDate' of null
TypeError: Cannot read property 'startDate' of null

请,帮助,我需要什么来工作DateRangePicker

1 个答案:

答案 0 :(得分:0)

您应该将state定义为组件的字段。默认情况下,如果未设置state,则等于null

import React from 'react'
import { DateRangePicker } from "@blueprintjs/datetime";

class MyAwesomeComponent extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            startDate: new Date("2018-05-01T12:13:30.643Z"),
            endDate: new Date("2018-05-03T12:13:30.643Z")
        }
    }

    render() {
        return (
            <div className="my-component">
                <DateRangePicker
                    value={[this.state.startDate, this.state.endDate]}
                    onChange={this.handleDateChange}
                />
            </div>
        )
    }
}
相关问题