Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type error for startDate and endDate props #5259

Open
SergeyKazarinov opened this issue Dec 3, 2024 · 0 comments
Open

type error for startDate and endDate props #5259

SergeyKazarinov opened this issue Dec 3, 2024 · 0 comments

Comments

@SergeyKazarinov
Copy link
Contributor

I use selectsRange props. If I pass startDate and endDate props, I get a type error because it expects Date or undefined type. However, the initial state for startDate and endDate is null. However, for a normal state (without selectsRange), the null type does not throw a type error.

Example

const DateInput = () => {
  const [rangeDate, setRangeDate] = useState<[Date | null, Date | null]>([
    null,
    null,
  ]);
  const [startDate, endDate] = rangeDate;
  return (
    <DatePicker
      id="datePickerRange"
      selectsRange
      startDate={startDate}
      endDate={endDate}
      onChange={(dates: [Date | null, Date | null]) => {
        setRangeDate(dates);
      }}
    />
  );
};

export default DateInput;

image
image

I can solve this problem by adding undefined to the props, but it's not the best solution

const DateInput = () => {
  const [rangeDate, setRangeDate] = useState<[Date | null, Date | null]>([
    null,
    null,
  ]);
  const [startDate, endDate] = rangeDate;
  return (
    <DatePicker
      id="datePickerRange"
      selectsRange
      startDate={startDate ?? undefined}
      endDate={endDate ?? undefined}
      onChange={(dates: [Date | null, Date | null]) => {
        setRangeDate(dates);
      }}
    />
  );
};

export default DateInput;
SergeyKazarinov added a commit to SergeyKazarinov/react-datepicker that referenced this issue Dec 3, 2024
martijnrusschen added a commit that referenced this issue Dec 6, 2024
…-endDate

fix: types for startDate and endDate props (#5259)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant