From 20ce901b6edb7b1199c478f59b8b56327ca23f67 Mon Sep 17 00:00:00 2001 From: Alexandre Fauquette Date: Tue, 27 Sep 2022 14:51:21 +0200 Subject: [PATCH] use recommended approach --- .../src/CalendarPicker/CalendarPicker.tsx | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/x-date-pickers/src/CalendarPicker/CalendarPicker.tsx b/packages/x-date-pickers/src/CalendarPicker/CalendarPicker.tsx index 5cc24d3fc00d4..063059250a6ab 100644 --- a/packages/x-date-pickers/src/CalendarPicker/CalendarPicker.tsx +++ b/packages/x-date-pickers/src/CalendarPicker/CalendarPicker.tsx @@ -437,15 +437,12 @@ export const CalendarPicker = React.forwardRef(function CalendarPicker( }, ); - const prevOpenViewRef = React.useRef(openView); - React.useEffect(() => { - // Set focus to the button when switching from a view to another - if (prevOpenViewRef.current === openView) { - return; - } - prevOpenViewRef.current = openView; + const [prevOpenView, setPrevOpenView] = React.useState(openView); + + if (prevOpenView !== openView) { handleFocusedViewChange(openView)(true); - }, [openView, handleFocusedViewChange]); + setPrevOpenView(openView); + } return (