From dead4e826a91915dc5902c7faf83a58efec845d8 Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Wed, 28 Aug 2019 12:22:15 +0200 Subject: [PATCH] fix: fix stack nested in tab always getting reset --- packages/native/src/useScrollToTop.tsx | 4 +++- packages/stack/src/navigators/createStackNavigator.tsx | 8 +++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/native/src/useScrollToTop.tsx b/packages/native/src/useScrollToTop.tsx index e8b65f10..8ec40216 100644 --- a/packages/native/src/useScrollToTop.tsx +++ b/packages/native/src/useScrollToTop.tsx @@ -14,10 +14,12 @@ export default function useScrollToTop(ref: React.RefObject) { // We don't wanna import tab types here to avoid extra deps // in addition, there are multiple tab implementations navigation.addListener('tabPress', (e: EventArg<'tabPress'>) => { + const isFocused = navigation.isFocused(); + // Run the operation in the next frame so we're sure all listeners have been run // This is necessary to know if preventDefault() has been called requestAnimationFrame(() => { - if (navigation.isFocused() && !e.defaultPrevented && ref.current) { + if (isFocused && !e.defaultPrevented && ref.current) { // When user taps on already focused tab, scroll to top ref.current.scrollTo({ y: 0 }); } diff --git a/packages/stack/src/navigators/createStackNavigator.tsx b/packages/stack/src/navigators/createStackNavigator.tsx index 7c3591c2..74f14f5d 100644 --- a/packages/stack/src/navigators/createStackNavigator.tsx +++ b/packages/stack/src/navigators/createStackNavigator.tsx @@ -45,14 +45,12 @@ function StackNavigator({ () => navigation.addListener && navigation.addListener('tabPress', (e: EventArg<'tabPress'>) => { + const isFocused = navigation.isFocused(); + // Run the operation in the next frame so we're sure all listeners have been run // This is necessary to know if preventDefault() has been called requestAnimationFrame(() => { - if ( - state.index > 0 && - navigation.isFocused() && - !e.defaultPrevented - ) { + if (state.index > 0 && isFocused && !e.defaultPrevented) { // When user taps on already focused tab and we're inside the tab, // reset the stack to replicate native behaviour navigation.dispatch({