Skip to content

Commit 37bb95f

Browse files
committed
Simplify the code
1 parent d2aba26 commit 37bb95f

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {useIsFocused} from '@react-navigation/native';
22
import {FlashList} from '@shopify/flash-list';
33
import 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';
55
import {View} from 'react-native';
66
import type {ViewToken} from 'react-native';
77
import 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

Comments
 (0)