11

I am following the Ionic tutorial for react. I created the tutorial page and I am trying to add a datepicker element to it. This is my page right now:

import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
import React, { useState } from 'react';
import DatePicker from 'react-datepicker'
import "react-datepicker/dist/react-datepicker.css";

const Home: React.FC = () => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <IonPage>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Ionic Blank</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
        The world is your oyster 14.
        <p>
          If you get lost, the{' '}
          <a target="_blank" rel="noopener noreferrer" href="https://ionicframework.com/docs/">
            docs
          </a>{' '}
          will be your guide.
        </p>
        <DatePicker selected={startDate} onChange={date => setStartDate(date)} />
      </IonContent>
    </IonPage>
  );
};

export default Home;

The datepicker that I am using is from here and I used the first example in my page:

() => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker selected={startDate} onChange={date => setStartDate(date)} />
  );
};

However, I am getting the following error:

[react-scripts] Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction<Date>'.
[react-scripts]   Type 'null' is not assignable to type 'SetStateAction<Date>'.  TS2345
[react-scripts]     22 |           will be your guide.
[react-scripts]     23 |         </p>
[react-scripts]   > 24 |         <DatePicker selected={startDate} onChange={date => setStartDate(date)} />
[react-scripts]        |                                                                         ^
[react-scripts]     25 |       </IonContent>
[react-scripts]     26 |     </IonPage>
[react-scripts]     27 |   );

What is the problem?

ford04
  • 66,267
  • 20
  • 199
  • 171
Tasos
  • 1,575
  • 5
  • 18
  • 44

5 Answers5

21

The onChange callback from react-datepicker looks like this:

onChange(date: Date | null, event: React.SyntheticEvent<any> | undefined): void;

So date potentially can be null. You have two options here:


1.) accept nullable date state in useState Hook:

const [startDate, setStartDate] = useState<Date | null>(new Date());

2.) only invoke setStartDate, when date is not null:

<DatePicker selected={startDate} onChange={date => date && setStartDate(date)} />
ford04
  • 66,267
  • 20
  • 199
  • 171
  • 2
    I get a weird one in 2021 now, Type 'Date | [Date | null, Date | null] | null' is not assignable to type 'Date | null | undefined'. for "selected" prop even .. hmmph? does not seem right, why not? :D – trainoasis Sep 23 '21 at 16:36
9

Update for same issue in 2021

<DatePicker selected={startDate} onChange={(date: Date | null) => setStartDate(date)} />

Does the job

Dupflo
  • 305
  • 3
  • 7
  • 1
    nope, still getting: `Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction'. Type 'null' is not assignable to type 'SetStateAction` – Mendi Sterenfeld Jun 02 '21 at 17:08
  • 1
    Yes because you have set your state like that const [startDate, setStartDate] = useStateuseState(null) in the case you plan to have "null" value – Dupflo Jun 03 '21 at 20:24
2

I know I am late to join the party, I just got the DatePicker working. Following is the working code:

const [startDate, setStartDate] = useState<Date>(new Date());
<DatePicker
   selected={startDate}
   onChange={(date: Date) => setStartDate(date!)}
/>
Jaisal Shah
  • 183
  • 1
  • 8
2

This works for me:

() => {
  const [startDate, setStartDate] = useState<Date|null>(new Date());
  return (
    <DatePicker selected={startDate} onChange={(date: Date | null) => setStartDate(date)} />
  );
};

user134097
  • 21
  • 2
0

For someone who get the error:

Type 'Date | [Date | null, Date | null] | null' is not assignable to type 'Date | null | undefined

That's because you are using date range so onChange argument will get an array type [start, end].

BaalWu
  • 21
  • 2