11import { useIsFocused } from '@react-navigation/native' ;
22import { FlashList } from '@shopify/flash-list' ;
33import type { FlashListRef , ListRenderItemInfo } from '@shopify/flash-list' ;
4- import React , { useCallback , useDeferredValue , useEffect , useMemo , useRef , useState } from 'react' ;
4+ import React , { useCallback , useDeferredValue , useEffect , useEffectEvent , useMemo , useRef , useState } from 'react' ;
55import { View } from 'react-native' ;
66import type { ViewToken } from 'react-native' ;
77import type { OnyxEntry } from 'react-native-onyx' ;
@@ -376,11 +376,9 @@ function MoneyRequestReportPreviewContent({
376376 } , [ carouselTransactions ] ) ;
377377
378378 const isFocused = useIsFocused ( ) ;
379- const isFocusedRef = useRef ( isFocused ) ;
380-
381- useEffect ( ( ) => {
382- isFocusedRef . current = isFocused ;
383- } , [ isFocused ] ) ;
379+ const getIsFocused = useEffectEvent ( ( ) => {
380+ return isFocused ;
381+ } ) ;
384382
385383 useEffect ( ( ) => {
386384 const index = carouselTransactions . findIndex ( ( transaction ) => newTransactionIDs ?. has ( transaction . transactionID ) ) ;
@@ -390,9 +388,10 @@ function MoneyRequestReportPreviewContent({
390388 }
391389 const newTransaction = carouselTransactions . at ( index ) ;
392390 setTimeout ( ( ) => {
393- if ( ! isFocusedRef . current ) {
391+ if ( ! getIsFocused ( ) ) {
394392 return ;
395393 }
394+
396395 // If the new transaction is not available at the index it was on before the delay, avoid the scrolling
397396 // because we are scrolling to either a wrong or unavailable transaction (which can cause crash).
398397 if ( newTransaction ?. transactionID !== carouselTransactionsRef . current . at ( index ) ?. transactionID ) {
0 commit comments