[DayRangePicker] onChange function call is not calld directly after selected dates
See original GitHub issueHey 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.
- Selecting a day range => array with selected days is still empty
- 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:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top 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 >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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
@hieu-t-code There’s a problem with your approach. Currently, you try to access
selectedDates
inhandleDaySelection
(handler foronChange
), but the thing is setting state in react is async. Therefore immediately after updating the state inhandleDaySelection
, you can’t access the latest updated value! There are three solutions for this which I presented in my forked CodeSandBox: