Skip to content

Commit e808253

Browse files
authored
Merge pull request #1942 from wix/infra/ContextProvider_performance
ContextProvider - wrap callbacks
2 parents 07c26a2 + 28a2a76 commit e808253

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/expandableCalendar/Context/Provider.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
5454
const {
5555
theme,
5656
date,
57+
onDateChanged,
58+
onMonthChange,
5759
showTodayButton = false,
5860
todayBottomMargin,
5961
todayButtonStyle,
@@ -66,8 +68,8 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
6668
const buttonY = useRef(new Animated.Value(todayBottomMargin ? -todayBottomMargin : -TOP_POSITION));
6769
const opacity = useRef(new Animated.Value(1));
6870
const today = useRef(getTodayFormatted());
69-
70-
const [prevDate, setPrevDate] = useState(date);
71+
const prevDate = useRef(date);
72+
const currDate = useRef(date); // for setDate only to keep prevDate up to date
7173
const [currentDate, setCurrentDate] = useState(date);
7274
const [updateSource, setUpdateSource] = useState(UpdateSources.CALENDAR_INIT);
7375
const [isDisabled, setIsDisabled] = useState(false);
@@ -89,38 +91,39 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
8991

9092
/** Context */
9193

92-
const _setDate = (date: string, updateSource: UpdateSources) => {
93-
setPrevDate(currentDate);
94+
const _setDate = useCallback((date: string, updateSource: UpdateSources) => {
95+
prevDate.current = currDate.current;
96+
currDate.current = date;
9497
setCurrentDate(date);
9598
setUpdateSource(updateSource);
9699
setButtonIcon(getButtonIcon(date, showTodayButton));
97100

98-
props.onDateChanged?.(date, updateSource);
101+
onDateChanged?.(date, updateSource);
99102

100-
if (!sameMonth(new XDate(date), new XDate(currentDate))) {
101-
props.onMonthChange?.(xdateToData(new XDate(date)), updateSource);
103+
if (!sameMonth(new XDate(date), new XDate(date))) {
104+
onMonthChange?.(xdateToData(new XDate(date)), updateSource);
102105
}
103-
};
106+
}, [onDateChanged, onMonthChange]);
104107

105-
const _setDisabled = (disabled: boolean) => {
108+
const _setDisabled = useCallback((disabled: boolean) => {
106109
if (!showTodayButton || disabled === isDisabled) {
107110
return;
108111
}
109112
setIsDisabled(disabled);
110113
animateOpacity(disabled);
111-
};
114+
}, [showTodayButton]);
112115

113116
const contextValue = useMemo(() => {
114117
return {
115118
date: currentDate,
116-
prevDate: prevDate,
119+
prevDate: prevDate.current,
117120
updateSource: updateSource,
118121
setDate: _setDate,
119122
setDisabled: _setDisabled,
120123
numberOfDays,
121124
timelineLeftInset
122125
};
123-
}, [currentDate, prevDate, updateSource, numberOfDays]);
126+
}, [currentDate, updateSource, numberOfDays]);
124127

125128
/** Animations */
126129

0 commit comments

Comments
 (0)