diff --git a/src/components/PopoverMenu.tsx b/src/components/PopoverMenu.tsx index e9df68d43d06..0846e27100da 100644 --- a/src/components/PopoverMenu.tsx +++ b/src/components/PopoverMenu.tsx @@ -1,6 +1,6 @@ import lodashIsEqual from 'lodash/isEqual'; import type {RefObject} from 'react'; -import React, {useEffect, useState} from 'react'; +import React, {useLayoutEffect, useState} from 'react'; import {StyleSheet, View} from 'react-native'; import type {ModalProps} from 'react-native-modal'; import useArrowKeyFocusManager from '@hooks/useArrowKeyFocusManager'; @@ -194,7 +194,10 @@ function PopoverMenu({ setFocusedIndex(-1); }; - useEffect(() => { + // When the menu items are changed, we want to reset the sub-menu to make sure + // we are not accessing the wrong sub-menu parent or possibly undefined when rendering the back button. + // We use useLayoutEffect so the reset happens before the repaint + useLayoutEffect(() => { if (menuItems.length === 0) { return; }