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.

[DayRangePicker] onChange function call is not calld directly after selected dates

See original GitHub issue

Hey guys,

so I have the following issue. I included a day range picker to my project. I want to do some stuff with all selected day between the selected day range. The thing is that the array is not filled with the days, unless I selected a day range and then reselecting one day.

  1. Selecting a day range => array with selected days is still empty
  2. Reseleicting one day => the array is filled with the previous selected day range.

here is my code:

//this function pushes the dates in the date array
function getChartCategories(startDate: Date, endDate: Date): Array<string> {
    const dates: Array<string> = [];
    const currentDate = new Date(startDate);
    let currentMonth: number;
    while (currentDate <= endDate) {
        currentMonth = calculateCurrentMonth(currentDate.getMonth());
        dates.push(formatDateToIso8601(currentDate, currentMonth));
        currentDate.setHours(0);
        currentDate.setDate(currentDate.getDate() + 1);
    }
    return dates;
}

function createStartDate(dayRange: DayRange): Date {
    return new Date(
        dayRange?.from?.year?.toString() +
            '-' +
            dayRange?.from?.month?.toString() +
            '-' +
            dayRange?.from?.day?.toString(),
    );
}

function createEndDate(dayRange: DayRange): Date {
    return new Date(
        dayRange?.to?.year?.toString() + '-' + dayRange?.to?.month?.toString() + '-' + dayRange?.to?.day?.toString(),
    );
}


const selectedDates = getChartCategories(createStartDate(selectedDayRange), createEndDate(selectedDayRange));
foo(){
console.Log('123') // is executed after each selection
//do some stuff with the selected days (all days are in an string array)
}
onChange = {v =>
foo();
setSelectedDayRange(v);}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
hieu-t-codecommented, Apr 30, 2020

I had in mind that it could be async, and couldn’t use the first solution as the const is needed somewhere else in my code!

The other two solutions are exactly what I needed. Thanks a lot for your time!

2reactions
Kiarash-Zcommented, Apr 30, 2020

@hieu-t-code There’s a problem with your approach. Currently, you try to access selectedDates in handleDaySelection(handler for onChange), but the thing is setting state in react is async. Therefore immediately after updating the state in handleDaySelection, you can’t access the latest updated value! There are three solutions for this which I presented in my forked CodeSandBox:

function handleDaySelection(days: DayRange) {
  setSelectedDayRange(days);
  // solution 1 ✅: move selectedDates to here and pass days: 
  // const selectedDates = getDays(createStartDate(days.from), createEndDate(days.to));
}
console.log(selectedDates); // solution 2 ✅: log it outside of onChange handler

// solution 3 ✅: useEffect!
// React.useEffect(() => {
//   if (selectedDayRange.from && selectedDayRange.to) {
//     // do stuff
//   }
// }, [selectedDayRange])
Read more comments on GitHub >

github_iconTop Results From Across the Web

onchange not calling function on a datepicker - Stack Overflow
Any ideas why onchange doesn't work after selecting a date. When the date gets selected, the field does in fact change. <div class="sectionData"> ......
Read more >
DatePicker is not firing OnChange Event in UI for Blazor - Telerik
The OnChange event represents a user action - confirmation of the current value. It fires when the user presses Enter in the input,...
Read more >
DatePicker - Mantine
Capture date or dates range from user.
Read more >
How to set a callback function in datepicker onselect
Then, in your JS change handler, you can just call dateSelected(<some date>) and the server-side function will get called! Antonio.
Read more >
React Multi Date Picker - Events
Each time the popper(calendar) position is recalculated, the onChange function is executed in the element-popper. Because onChange does something else in ...
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