Closed CorentinHeroux closed 2 years ago
@CorentinHeroux
I'm implementing a "Today" button and the same problem happened to me. And with React Native Debugger, I found that in Calendar its props.current changes accordingly (that's current) but state.currentDate does not. I think that's why there is no re-rendering of the month.
I found this issue #764 can be a solution with ref and Calendar.addMonth().
With my modification, it becomes
function todayBtnOnPress() {
var today = new Date();
var selectedDate = new Date(currentDate);
var diff = - (selectedDate.getMonth() - today.getMonth()) - (selectedDate.getFullYear() - today.getFullYear()) * 12
this.calendar.addMonth(diff);
// need to keep track of current date when swipe/date or month changes
// for month difference calculation and therefore jump/refresh
setCurrentDate(formatDate(today));
};
function onDayPress(date) {
// need to keep track of current date when swipe/date or month changes
// for month difference calculation and therefore jump/refresh
setCurrentDate(date.dateString);
};
function onMonthChange(date) {
// need to keep track of current date when swipe/date or month changes
// for month difference calculation and therefore jump/refresh
setCurrentDate(date.dateString);
};
<Button
title='Today'
onPress={() => todayBtnOnPress()}
/>
const [currentDate, setCurrentDate] = useState(formatDate(new Date()));
<Calendar
horizontal={true}
pagingEnabled={true}
enableSwipeMonths={true}
// current={currentDate}
ref={ref => this.calendar=ref}
onDayPress={onDayPress}
onMonthChange={onMonthChange}
/>
Try passing key={current}
to your Calendar
. It works for me
@kelokchan May I know what is current in key={current}
?
@kelokchan May I know what is current in
key={current}
?
Should be the same as the current
prop you pass into the Calendar
, which is the currently selected date.
@kelokchan May I know what is current in
key={current}
?Should be the same as the
current
prop you pass into theCalendar
, which is the currently selected date.
you saved me hours dude!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Try passing
key={current}
to yourCalendar
. It works for me
It works too, but May I ask why is it happen ?
Try passing
key={current}
to yourCalendar
. It works for me
Thanks man!
Try passing
key={current}
to yourCalendar
. It works for me
Glad this is working for some, but definitely not working for me. Given that current
needs to be a Date
type and key
is a string
, I'm not sure how this is supposed to work.
The type docs also say that we shouldn't need to use this. We shouldn't need to utilize an internal attribute for something as basic as programmatically selecting the selected date in a calendar. Might be worth discussing how we can get a proper fix this?
Try passing
key={current}
to yourCalendar
. It works for meGlad this is working for some, but definitely not working for me. Given that
current
needs to be aDate
type andkey
is astring
, I'm not sure how this is supposed to work.The type docs also say that we shouldn't need to use this. We shouldn't need to utilize an internal attribute for something as basic as programmatically selecting the selected date in a calendar. Might be worth discussing how we can get a proper fix this?
Hi, you can use string
type for the current
, e.g {'2021-09-06'}, you can try
Thanks for the reply @mickeylam, but no that doesn't work.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I've tried that key prop, it doesn't work Does anyone know how to jump to a selected month?
@kelokchan Thanks a lot dude. This small hack served my time. I appreciate :)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Awesome! This key={} works :)
For me passing key={new Date()} works !
I need to change the current year for the calendar display. I make this possible with a button that updates the value of "current". The calendar doesn't update, but "current" does. How can I do this?
Here's my example that doesn't work :
"react": "16.11.0", "react-native": "^0.62.2", "react-native-calendars": "^1.403.0",