React-dates: onDatesChange returns null

Created on 24 Mar 2020  路  6Comments  路  Source: airbnb/react-dates

This is code that I implemented

<DayPickerRangeController
                startDate={startDate}
                startDateId="startDateId"
                endDate={endDate}
                endDateId="endDateId"
                onDatesChange={(date) => console.log(date)}
                focusedInput={focus}
                onFocusChange={(focus) => console.log(focus)}
              />

But onDatesChange returns null

{startDate: null, endDate: null}

here are my hooks

const [dateRange, setDateRange] = useState({
    startDate: null,
    endDate: null,
  });
  const [focus, setFocus] = useState(null);

const { startDate, endDate } = dateRange;

Any ideas why?

These are my imports

import 'react-dates/initialize';
import { DayPickerRangeController } from 'react-dates';
question

All 6 comments

@M1ck0 According to docs
The user will only be able to select a date if focusedInput is provided.
So your focus should be startDate

If you think this

focusedInput={startDate}

It is not working.. Already tried that

@M1ck0 it should be: focusedInput='startDate'
You code will be:

const [dateRange, setDateRange] = React.useState({
    startDate: null,
    endDate: null
  });

  const [focus, setFocus] = React.useState("startDate");

  const { startDate, endDate } = dateRange;

  return (
    <div className="App">
      <DayPickerRangeController
        startDate={startDate}
        endDate={endDate}
        onDatesChange={date => setDateRange(date)}
        focusedInput={focus}
        onFocusChange={focus => setFocus(focus)}
      />
    </div>
  );

It works (sort of). It does what it should but now I can't change date.
Any ideas?

If you want to change date set focus to 'startDate'

setFocus('startDate')

I was running into a similar issue. The previous solution worked for me with a _minor_ adjustment, which was pointed out here: https://github.com/airbnb/react-dates/issues/736#issuecomment-333237624

According to that, I adjusted the callback for the onFocusChange prop to the following:

...
onFocusChange={(focus) => setFocus(focus || "startDate")}
...

Based on the previous comments, my solution looks like the following:

  const [dateRange, setDateRange] = React.useState({
    startDate: null,
    endDate: null,
  });

  const [focus, setFocus] = React.useState("startDate");
  const { startDate, endDate } = dateRange;

  return (
    <div className="App">
      <DayPickerRangeController
        startDate={startDate}
        endDate={endDate}
        onDatesChange={(date) => setDateRange(date)}
        focusedInput={focus}
        onFocusChange={(focus) => setFocus(focus || "startDate")}
      />
    </div>
  );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sag1v picture sag1v  路  3Comments

yokomizor picture yokomizor  路  3Comments

Daniel15 picture Daniel15  路  3Comments

prztrz picture prztrz  路  3Comments

augnustin picture augnustin  路  3Comments