|
1 | | -import {useFocusEffect} from '@react-navigation/native'; |
| 1 | +import {useIsFocused} from '@react-navigation/native'; |
2 | 2 | import {hasSeenTourSelector} from '@selectors/Onboarding'; |
3 | 3 | import {FlashList} from '@shopify/flash-list'; |
4 | 4 | import type {FlashListRef, ListRenderItemInfo} from '@shopify/flash-list'; |
@@ -552,31 +552,40 @@ function MoneyRequestReportPreviewContent({ |
552 | 552 | carouselTransactionsRef.current = carouselTransactions; |
553 | 553 | }, [carouselTransactions]); |
554 | 554 |
|
555 | | - useFocusEffect( |
556 | | - useCallback(() => { |
557 | | - const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.has(transaction.transactionID)); |
| 555 | + const isFocused = useIsFocused(); |
| 556 | + const isFocusedRef = useRef(isFocused); |
558 | 557 |
|
559 | | - if (index < 0) { |
| 558 | + useEffect(() => { |
| 559 | + isFocusedRef.current = isFocused; |
| 560 | + }, [isFocused]); |
| 561 | + |
| 562 | + useEffect(() => { |
| 563 | + const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.has(transaction.transactionID)); |
| 564 | + |
| 565 | + if (index < 0) { |
| 566 | + return; |
| 567 | + } |
| 568 | + const newTransaction = carouselTransactions.at(index); |
| 569 | + setTimeout(() => { |
| 570 | + if (!isFocusedRef.current) { |
| 571 | + return; |
| 572 | + } |
| 573 | + // If the new transaction is not available at the index it was on before the delay, avoid the scrolling |
| 574 | + // because we are scrolling to either a wrong or unavailable transaction (which can cause crash). |
| 575 | + if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) { |
560 | 576 | return; |
561 | 577 | } |
562 | | - const newTransaction = carouselTransactions.at(index); |
563 | | - setTimeout(() => { |
564 | | - // If the new transaction is not available at the index it was on before the delay, avoid the scrolling |
565 | | - // because we are scrolling to either a wrong or unavailable transaction (which can cause crash). |
566 | | - if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) { |
567 | | - return; |
568 | | - } |
569 | 578 |
|
570 | | - carouselRef.current?.scrollToIndex({ |
571 | | - index, |
572 | | - viewOffset: -2 * styles.gap2.gap, |
573 | | - animated: true, |
574 | | - }); |
575 | | - }, CONST.ANIMATED_TRANSITION); |
| 579 | + carouselRef.current?.scrollToIndex({ |
| 580 | + index, |
| 581 | + viewOffset: -2 * styles.gap2.gap, |
| 582 | + animated: true, |
| 583 | + }); |
| 584 | + }, CONST.ANIMATED_TRANSITION); |
576 | 585 |
|
577 | | - // eslint-disable-next-line react-hooks/exhaustive-deps |
578 | | - }, [newTransactionIDs]), |
579 | | - ); |
| 586 | + // We only want to scroll to a new transaction when the set of new transaction IDs changes. |
| 587 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 588 | + }, [newTransactionIDs]); |
580 | 589 |
|
581 | 590 | const onViewableItemsChanged = useRef(({viewableItems}: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { |
582 | 591 | const newIndex = viewableItems.at(0)?.index; |
|
0 commit comments