@@ -6,6 +6,7 @@ import React, { useEffect, useRef } from "react";
6
6
import usePortal from "react-useportal" ;
7
7
8
8
import ContextualMenuDropdown from "./ContextualMenuDropdown" ;
9
+ import { usePrevious } from "app/base/hooks" ;
9
10
10
11
const ContextualMenu = ( {
11
12
className,
@@ -29,7 +30,7 @@ const ContextualMenu = ({
29
30
const { openPortal, closePortal, isOpen, Portal } = usePortal ( {
30
31
isOpen : ! hasToggle ,
31
32
} ) ;
32
- const previousIsOpen = useRef ( isOpen ) ;
33
+ const previousIsOpen = usePrevious ( isOpen ) ;
33
34
const labelNode = toggleLabel ? < span > { toggleLabel } </ span > : null ;
34
35
const wrapperClass = classNames (
35
36
className ,
@@ -42,9 +43,8 @@ const ContextualMenu = ({
42
43
// The isOpen state is compared to the previous state so that we can
43
44
// initialise the previous state in a way that means that this effect does
44
45
// not call onToggleMenu on first render.
45
- if ( isOpen !== previousIsOpen . current ) {
46
+ if ( isOpen !== previousIsOpen ) {
46
47
onToggleMenu && onToggleMenu ( isOpen ) ;
47
- previousIsOpen . current = isOpen ;
48
48
}
49
49
// onToggleMenu is excluded from the useEffect deps as onToggleMenu gets
50
50
// redefined on a state update which causes an infinite loop here.
0 commit comments