1
1
import { useCallback , useEffect , useMemo , useState } from 'react' ;
2
2
import CONST from '@src/CONST' ;
3
3
import useKeyboardShortcut from './useKeyboardShortcut' ;
4
+ import usePrevious from './usePrevious' ;
4
5
5
6
type Config = {
6
7
maxIndex : number ;
@@ -51,6 +52,7 @@ export default function useArrowKeyFocusManager({
51
52
isFocused = true ,
52
53
} : Config ) : UseArrowKeyFocusManager {
53
54
const [ focusedIndex , setFocusedIndex ] = useState ( initialFocusedIndex ) ;
55
+ const prevIsFocusedIndex = usePrevious ( focusedIndex ) ;
54
56
const arrowConfig = useMemo (
55
57
( ) => ( {
56
58
excludedNodes : shouldExcludeTextAreaNodes ? [ 'TEXTAREA' ] : [ ] ,
@@ -67,8 +69,13 @@ export default function useArrowKeyFocusManager({
67
69
[ isActive , shouldExcludeTextAreaNodes , allowHorizontalArrowKeys ] ,
68
70
) ;
69
71
70
- // eslint-disable-next-line react-hooks/exhaustive-deps
71
- useEffect ( ( ) => onFocusedIndexChange ( focusedIndex ) , [ focusedIndex ] ) ;
72
+ useEffect ( ( ) => {
73
+ if ( prevIsFocusedIndex === focusedIndex ) {
74
+ return ;
75
+ }
76
+ onFocusedIndexChange ( focusedIndex ) ;
77
+ // eslint-disable-next-line react-hooks/exhaustive-deps
78
+ } , [ focusedIndex , prevIsFocusedIndex ] ) ;
72
79
73
80
const arrowUpCallback = useCallback ( ( ) => {
74
81
if ( maxIndex < 0 || ! isFocused ) {
0 commit comments