Skip to content

Commit 5c48f04

Browse files
committed
fix invoking callbacks on mount
1 parent ea81a4e commit 5c48f04

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

example/src/screens/expandableCalendar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export default class ExpandableCalendarScreen extends Component<Props> {
181181
render() {
182182
return (
183183
<CalendarProvider
184-
date={ITEMS[0].title}
184+
date={ITEMS[1].title}
185185
onDateChanged={this.onDateChanged}
186186
onMonthChange={this.onMonthChange}
187187
showTodayButton

src/calendar/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const Calendar = (props: CalendarProps) => {
8181
const [currentMonth, setCurrentMonth] = useState(current || initialDate ? parseDate(current || initialDate) : new XDate());
8282
const style = useRef(styleConstructor(theme));
8383
const header = useRef();
84+
const isMounted = useRef(false);
85+
8486

8587
useEffect(() => {
8688
if (initialDate) {
@@ -89,9 +91,14 @@ const Calendar = (props: CalendarProps) => {
8991
}, [initialDate]);
9092

9193
useEffect(() => {
92-
const _currentMonth = currentMonth.clone();
93-
onMonthChange?.(xdateToData(_currentMonth));
94-
onVisibleMonthsChange?.([xdateToData(_currentMonth)]);
94+
if (isMounted.current) {
95+
// Avoid callbacks call on mount
96+
const _currentMonth = currentMonth.clone();
97+
onMonthChange?.(xdateToData(_currentMonth));
98+
onVisibleMonthsChange?.([xdateToData(_currentMonth)]);
99+
} else {
100+
isMounted.current = true;
101+
}
95102
}, [currentMonth]);
96103

97104
const updateMonth = (newMonth: XDate) => {

0 commit comments

Comments
 (0)