|
1 | | -import {useFocusEffect} from '@react-navigation/native'; |
| 1 | +import {useIsFocused} from '@react-navigation/native'; |
2 | 2 | import {FlashList} from '@shopify/flash-list'; |
3 | 3 | 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'; |
5 | 5 | import {View} from 'react-native'; |
6 | 6 | import type {ViewToken} from 'react-native'; |
7 | 7 | import type {OnyxEntry} from 'react-native-onyx'; |
@@ -375,31 +375,39 @@ function MoneyRequestReportPreviewContent({ |
375 | 375 | carouselTransactionsRef.current = carouselTransactions; |
376 | 376 | }, [carouselTransactions]); |
377 | 377 |
|
378 | | - useFocusEffect( |
379 | | - useCallback(() => { |
380 | | - const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.has(transaction.transactionID)); |
| 378 | + const isFocused = useIsFocused(); |
| 379 | + const getIsFocused = useEffectEvent(() => { |
| 380 | + return isFocused; |
| 381 | + }); |
| 382 | + |
| 383 | + useEffect(() => { |
| 384 | + const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.has(transaction.transactionID)); |
381 | 385 |
|
382 | | - if (index < 0) { |
| 386 | + if (index < 0) { |
| 387 | + return; |
| 388 | + } |
| 389 | + const newTransaction = carouselTransactions.at(index); |
| 390 | + setTimeout(() => { |
| 391 | + if (!getIsFocused()) { |
383 | 392 | return; |
384 | 393 | } |
385 | | - const newTransaction = carouselTransactions.at(index); |
386 | | - setTimeout(() => { |
387 | | - // If the new transaction is not available at the index it was on before the delay, avoid the scrolling |
388 | | - // because we are scrolling to either a wrong or unavailable transaction (which can cause crash). |
389 | | - if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) { |
390 | | - return; |
391 | | - } |
392 | | - |
393 | | - carouselRef.current?.scrollToIndex({ |
394 | | - index, |
395 | | - viewOffset: -2 * styles.gap2.gap, |
396 | | - animated: true, |
397 | | - }); |
398 | | - }, CONST.ANIMATED_TRANSITION); |
399 | | - |
400 | | - // eslint-disable-next-line react-hooks/exhaustive-deps |
401 | | - }, [newTransactionIDs]), |
402 | | - ); |
| 394 | + |
| 395 | + // If the new transaction is not available at the index it was on before the delay, avoid the scrolling |
| 396 | + // because we are scrolling to either a wrong or unavailable transaction (which can cause crash). |
| 397 | + if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) { |
| 398 | + return; |
| 399 | + } |
| 400 | + |
| 401 | + carouselRef.current?.scrollToIndex({ |
| 402 | + index, |
| 403 | + viewOffset: -2 * styles.gap2.gap, |
| 404 | + animated: true, |
| 405 | + }); |
| 406 | + }, CONST.ANIMATED_TRANSITION); |
| 407 | + |
| 408 | + // We only want to scroll to a new transaction when the set of new transaction IDs changes. |
| 409 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 410 | + }, [newTransactionIDs]); |
403 | 411 |
|
404 | 412 | const onViewableItemsChanged = useRef(({viewableItems}: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { |
405 | 413 | const newIndex = viewableItems.at(0)?.index; |
|
0 commit comments