Skip to content

Commit

Permalink
Add onChangeRef to prevent stale closure
Browse files Browse the repository at this point in the history
  • Loading branch information
cxo05 committed Aug 17, 2023
1 parent ad50cef commit 9577185
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import PrimeReact, { PrimeReactContext, localeOption, localeOptions } from '../api/Api';
import { Button } from '../button/Button';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMountEffect, useOverlayListener, usePrevious, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { CalendarIcon } from '../icons/calendar';
import { ChevronDownIcon } from '../icons/chevrondown';
Expand All @@ -13,7 +14,6 @@ import { Ripple } from '../ripple/Ripple';
import { DomHandler, IconUtils, ObjectUtils, UniqueComponentId, ZIndexUtils, classNames, mask, mergeProps } from '../utils/Utils';
import { CalendarBase } from './CalendarBase';
import { CalendarPanel } from './CalendarPanel';
import { useHandleStyle } from '../componentbase/ComponentBase';

export const Calendar = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -47,6 +47,7 @@ export const Calendar = React.memo(
const ignoreMaskChange = React.useRef(false);
const previousButton = React.useRef(false);
const nextButton = React.useRef(false);
const onChangeRef = React.useRef(null);

const [currentView, setCurrentView] = React.useState('date');
const [currentMonth, setCurrentMonth] = React.useState(null);
Expand Down Expand Up @@ -1424,7 +1425,7 @@ export const Calendar = React.memo(

viewStateChanged.current = true;

props.onChange({
onChangeRef.current({
originalEvent: event,
value: newValue,
stopPropagation: () => {
Expand Down Expand Up @@ -2504,6 +2505,10 @@ export const Calendar = React.memo(
}
});

React.useEffect(() => {
onChangeRef.current = props.onChange;
}, [props.onChange]);

React.useEffect(() => {
let unbindMaskEvents = null;

Expand Down

0 comments on commit 9577185

Please sign in to comment.