Skip to content

Commit 10ca7e5

Browse files
committed
ExpandableCalendar - fix double render on load
1 parent a17d9e3 commit 10ca7e5

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

example/src/screens/expandableCalendarScreen.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const ExpandableCalendarScreen = (props: Props) => {
6363
markedDates={marked.current}
6464
leftArrowImageSource={leftArrowIcon}
6565
rightArrowImageSource={rightArrowIcon}
66-
onMonthChange={onMonthChange}
6766
// animateScroll
6867
// closeOnDayPress={false}
6968
/>

src/calendar-list/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ const CalendarList = (props: CalendarListProps, ref: any) => {
101101
const headerProps = extractHeaderProps(props);
102102
const calendarSize = horizontal ? calendarWidth : calendarHeight;
103103

104+
const [currentMonth, setCurrentMonth] = useState(parseDate(current));
105+
104106
const style = useRef(styleConstructor(theme));
105107
const list = useRef();
106108
const range = useRef(horizontal ? 1 : 3);
107109
const initialDate = useRef(parseDate(current));
108-
const visibleMonth = useRef();
109-
110-
const [currentMonth, setCurrentMonth] = useState(parseDate(current));
110+
const visibleMonth = useRef(currentMonth);
111111

112112
const items = useMemo(() => {
113113
const months = [];

src/calendar/index.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {extractHeaderProps, extractDayProps} from '../componentUpdater';
1414
// @ts-expect-error
1515
import {WEEK_NUMBER} from '../testIDs';
1616
import {DateData, Theme} from '../types';
17+
import {useDidUpdate} from '../hooks';
1718
import styleConstructor from './style';
1819
import CalendarHeader, {CalendarHeaderProps} from './header';
1920
import Day, {DayProps} from './day/index';
@@ -105,7 +106,6 @@ const Calendar = (props: CalendarProps) => {
105106
const [currentMonth, setCurrentMonth] = useState(current || initialDate ? parseDate(current || initialDate) : new XDate());
106107
const style = useRef(styleConstructor(theme));
107108
const header = useRef();
108-
const isMounted = useRef(false);
109109
const weekNumberMarking = useRef({disabled: true, disableTouchEvent: true});
110110

111111
useEffect(() => {
@@ -114,15 +114,10 @@ const Calendar = (props: CalendarProps) => {
114114
}
115115
}, [initialDate]);
116116

117-
useEffect(() => {
118-
if (isMounted.current) {
119-
// Avoid callbacks call on mount
120-
const _currentMonth = currentMonth.clone();
121-
onMonthChange?.(xdateToData(_currentMonth));
122-
onVisibleMonthsChange?.([xdateToData(_currentMonth)]);
123-
} else {
124-
isMounted.current = true;
125-
}
117+
useDidUpdate(() => {
118+
const _currentMonth = currentMonth.clone();
119+
onMonthChange?.(xdateToData(_currentMonth));
120+
onVisibleMonthsChange?.([xdateToData(_currentMonth)]);
126121
}, [currentMonth]);
127122

128123
const updateMonth = useCallback((newMonth: XDate) => {

src/expandableCalendar/Context/Provider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Animated, TouchableOpacity, View, ViewStyle, ViewProps, StyleProp} from
55

66
import {sameMonth} from '../../dateutils';
77
import {xdateToData} from '../../interface';
8+
import {useDidUpdate} from '../../hooks';
89
import {Theme, DateData} from '../../types';
910
import {UpdateSources} from '../commons';
1011
import styleConstructor from '../style';
@@ -79,7 +80,7 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
7980
return [style.current.contextWrapper, propsStyle];
8081
}, [style, propsStyle]);
8182

82-
useEffect(() => {
83+
useDidUpdate(() => {
8384
if (date) {
8485
_setDate(date, UpdateSources.PROP_UPDATE);
8586
}

0 commit comments

Comments
 (0)