javascriptreactjstypescriptnext.jsrsuite

How to fix the date[] is not assignable to type [date?, date?] in react hook


What I'm trying to do is to set a date on date range but there's an error which is

Type 'Date[]' is not assignable to type '[Date?, Date?]'.

Types of property 'length' are incompatible.

Type 'number' is not assignable to type '0 | 1 | 2'

code:

const [ dateRange, setDateRange] = useState([new Date('2017-02-01'), new Date('2017-05-20')]);
<DateRangePicker
                    value={dateRange}
                    onChange={value => {
                        this.setState({ value });
                        console.log(value);
                    }} />

Solution

  • The right way is to import the valuetype from rsuite

    import {ValueType} from "rsuite/lib/DateRangePicker";
    
    

    Then define it in your useState

    const [ dateRange, setDateRange] = useState<ValueType>([new Date('2017-02-01'), new Date('2017-05-20')])
    
    <DateRangePicker
        value={dateRange}
        onChange={value => {
        setDateRange(value);
        }}
    />