Skip to content

Commit

Permalink
fix(react-calendar-compat, Calendar): Calendar should memoize default…
Browse files Browse the repository at this point in the history
… value since it's a new Date() to avoid rerenders (microsoft#29747)

* fix: Calendar should memoize default value since it's a new Date() to avoid rerenders.

* change files

* Update change/@fluentui-react-f74bc323-4380-4898-a854-205fb9b839f3.json

* Update change/@fluentui-react-calendar-compat-b50cbe44-44ed-4b3a-9329-aa9be78d1e75.json
  • Loading branch information
sopranopillow authored Nov 3, 2023
1 parent 667d05d commit fb0dbce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(Calendar): Calendar should memoize today's default value since it causes rerenders by creating a new object each time.",
"packageName": "@fluentui/react-calendar-compat",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(Calendar): Calendar should memoize today's default value since it causes rerenders by creating a new object each time.",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ const defaultWorkWeekDays: DayOfWeek[] = [
DayOfWeek.Friday,
];

function useDateState({ value, today = new Date(), onSelectDate }: CalendarProps) {
function useDateState(props: CalendarProps) {
const { value, today: todayProp, onSelectDate } = props;

const today = React.useMemo(() => {
if (todayProp === undefined) {
return new Date();
}
return todayProp;
}, [todayProp]);

/** The currently selected date in the calendar */
const [selectedDate, setSelectedDate] = useControllableState({
defaultState: today,
Expand Down
11 changes: 10 additions & 1 deletion packages/react/src/components/Calendar/Calendar.base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ const DEFAULT_PROPS: Partial<ICalendarProps> = {
allFocusable: false,
};

function useDateState({ value, today = new Date(), onSelectDate }: ICalendarProps) {
function useDateState(props: ICalendarProps) {
const { value, today: todayProp, onSelectDate } = props;

const today = React.useMemo(() => {
if (todayProp === undefined) {
return new Date();
}
return todayProp;
}, [todayProp]);

/** The currently selected date in the calendar */
const [selectedDate = today, setSelectedDate] = useControllableValue(value, today);

Expand Down

0 comments on commit fb0dbce

Please sign in to comment.