Kevin
Kevin

Reputation: 37

How to fix the null problem at the "date"?

Argument of type 'Date | [Date, Date] | null' is not assignable to parameter of type 'SetStateAction<Date | null>'

const [startDate, setStartDate] = useState<Date | null>(new Date());
    return (
        <div className="rounded bg-night-sky p-5">
            <div className="d-flex align-items-center justify-content-center flex-wrap">
                <div className="m-4">
                    <DatePicker
                        selected={startDate}
                        onChange={(date) => setStartDate(date)}
                        showTimeSelect
                        timeFormat="HH:mm"
                        timeIntervals={20}
                        timeCaption="time"
                        dateFormat="MMMM d, yyyy h:mm aa"
                    />
                </div>
            </div>
        </div>
    );
};

Upvotes: 2

Views: 465

Answers (1)

lifeiscontent
lifeiscontent

Reputation: 593

try this:

const [startDate, setStartDate] = useState<Date | [Date, Date] | null>(new Date());
    return (
        <div className="rounded bg-night-sky p-5">
            <div className="d-flex align-items-center justify-content-center flex-wrap">
                <div className="m-4">
                    <DatePicker
                        selected={startDate}
                        onChange={(date) => setStartDate(date)}
                        showTimeSelect
                        timeFormat="HH:mm"
                        timeIntervals={20}
                        timeCaption="time"
                        dateFormat="MMMM d, yyyy h:mm aa"
                    />
                </div>
            </div>
        </div>
    );
};

Upvotes: 3

Related Questions