Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: fix stack nested in tab always getting reset
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 28, 2019
1 parent d5b4210 commit dead4e8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/native/src/useScrollToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export default function useScrollToTop(ref: React.RefObject<ScrollableView>) {
// 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 });
}
Expand Down
8 changes: 3 additions & 5 deletions packages/stack/src/navigators/createStackNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit dead4e8

Please sign in to comment.