Skip to content

Commit 28a2a76

Browse files
committed
Adding currDate ref in ref to avoid currentDate to change the setDate callback on every state change
1 parent 46a8e18 commit 28a2a76

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/expandableCalendar/Context/Provider.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
6969
const opacity = useRef(new Animated.Value(1));
7070
const today = useRef(getTodayFormatted());
7171
const prevDate = useRef(date);
72+
const currDate = useRef(date); // for setDate only to keep prevDate up to date
7273
const [currentDate, setCurrentDate] = useState(date);
7374
const [updateSource, setUpdateSource] = useState(UpdateSources.CALENDAR_INIT);
7475
const [isDisabled, setIsDisabled] = useState(false);
@@ -91,7 +92,8 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
9192
/** Context */
9293

9394
const _setDate = useCallback((date: string, updateSource: UpdateSources) => {
94-
prevDate.current = currentDate;
95+
prevDate.current = currDate.current;
96+
currDate.current = date;
9597
setCurrentDate(date);
9698
setUpdateSource(updateSource);
9799
setButtonIcon(getButtonIcon(date, showTodayButton));
@@ -101,7 +103,7 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
101103
if (!sameMonth(new XDate(date), new XDate(date))) {
102104
onMonthChange?.(xdateToData(new XDate(date)), updateSource);
103105
}
104-
}, [currentDate, onDateChanged, onMonthChange]);
106+
}, [onDateChanged, onMonthChange]);
105107

106108
const _setDisabled = useCallback((disabled: boolean) => {
107109
if (!showTodayButton || disabled === isDisabled) {

0 commit comments

Comments
 (0)