question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onDatesChange returns null

See original GitHub issue

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';

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Jeniffencommented, Jan 5, 2021

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>
  );
1reaction
mmarkelovcommented, Mar 24, 2020

@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>
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

onDatesChange returns null · Issue #1953 · react-dates/react ...
This is code that I implemented console.log(date)} focusedInput={focus} onFocusChange={(focus) => ... ... onDatesChange returns null #1953.
Read more >
Airbnb react-dates returns null - Stack Overflow
I've tested it in codesandbox and it seems that onFocusChange is not getting triggered once the focus variable ends up being null.
Read more >
react-dates onDatesChange startDate and endDate are null ...
Coding example for the question react-dates onDatesChange startDate and endDate are null, DayPickerRangeController-Reactjs.
Read more >
@datepicker-react/hooks - npm
useDatepickerProps. onDatesChange: (data: OnDatesChangeProps) => void. A callback is triggered when the date is selected ( onDaySelect ).
Read more >
DatePicker - Calcite React
return (. 27. <DatePicker. 28. id="basicDatePicker" ... {(touched.birthday && errors.birthday) || null} ... onDatesChange({ startDate, endDate }) {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found