Search code examples
javascriptreactjstypeerrordaterangepickergettime

react js blueprintjs Date Range Picker not work


I need create DateRangePicker on BlueprintJS(documentation) and RangePicker in my component, like in this screenshot.

I instalation all npm packages, and do all by instructions:

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

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

but anyway have error:

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

please, help, what i need for working DateRangePicker


Solution

  • You should define state as field of your component. By default, if state is not set, it equals to 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>
            )
        }
    }