0

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);
                    }} />

2 Answers2

0

The easiest but not "right" way just to make cast

this.setState({ value: value as [Date?, Date?]});

or

onChange={value:[Date, Date] => {

Better to create [Date?, Date?] variable inside your onChange function using the input value variable

Mike Malyi
  • 983
  • 8
  • 18
  • but I need to use the state cause I'll use it to other function when filtering the date. –  Feb 18 '21 at 10:31
  • 1
    There is no problem with the state itself. You just need to check input data to be sure that it will be saved in the right array size and format. – Mike Malyi Feb 18 '21 at 10:33
0

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);
    }}
/>      
enoch
  • 2,587
  • 2
  • 15
  • 22