question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

initialPage does not work on Android with Hermes

See original GitHub issue

Current Behavior

initialRouteName don’t work. Or scrolling to first route. Problem only on Android with Hermes (no problem on iOS with Hermes).

Expected Behavior

Tabs should open specified initialRouteName on init.

How to reproduce

https://github.com/Bardiamist/initialRouteRame

Your Environment

software version iOS or Android Android (Checked on Android 11 simulator) @react-navigation/native 5.9.3 @react-navigation/material-top-tabs 5.3.14 react-native-tab-view 3.0.0 react-native 0.64.0 expo - node 15.12.0 npm or yarn yarn 1.22.10

Original post: https://github.com/react-navigation/react-navigation/issues/9435#issuecomment-802644853

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
KusStarcommented, Mar 26, 2021

This works for me: https://github.com/callstack/react-native-pager-view/blob/master/src/PagerView.tsx#L64

@@ -61,7 +61,9 @@ export class ViewPager extends React.Component<ViewPagerProps> {
     // On iOS we do it directly on the native side
     if (Platform.OS === 'android') {
       if (this.props.initialPage != null) {
-        this.setPageWithoutAnimation(this.props.initialPage);
+        requestAnimationFrame(() => {
+          this.setPageWithoutAnimation(this.props.initialPage);
+        })
       }
     }
   }
4reactions
danilobuergercommented, Apr 29, 2021

Since I cant upgrade to rn-pager-view v5 at the moment (facebook/react-native#31245 happens with rn64 & pager-view v5, but not v4), I have patched v4 to fix the problem without a flash of the first page. Maybe someone wants to port this to v5:

diff --git a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java
index d2ca9b0..d4f5bd1 100644
--- a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java
+++ b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java
@@ -144,6 +144,8 @@ public class ReactViewPager extends VerticalViewPager {
   private final EventDispatcher mEventDispatcher;
   private boolean mIsCurrentItemFromJs;
   private boolean mScrollEnabled = true;
+  private int mInitialPage = 0; 
+  private boolean mInitialPageWasSet = false;
 
   public ReactViewPager(ReactContext reactContext) {
     super(reactContext);
@@ -212,6 +214,12 @@ public class ReactViewPager extends VerticalViewPager {
   @Override
   protected void onAttachedToWindow() {
     super.onAttachedToWindow();
+
+    if (!mInitialPageWasSet) {
+      setCurrentItemFromJs(mInitialPage, false);
+      mInitialPageWasSet = true;
+    }
+
     // The viewpager reset an internal flag on this method so we need to run another layout pass
     // after attaching to window.
     this.requestLayout();
@@ -228,6 +236,10 @@ public class ReactViewPager extends VerticalViewPager {
     }
   };
 
+  /*package*/ void setInitialPage(int initialPage) {
+    mInitialPage = initialPage;
+  }
+
   /*package*/ void addViewToAdapter(View child, int index) {
     getAdapter().addView(child, index);
   }
diff --git a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPagerManager.java b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPagerManager.java
index f8a4cb7..7a98e14 100644
--- a/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPagerManager.java
+++ b/node_modules/react-native-pager-view/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPagerManager.java
@@ -146,4 +146,9 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
         pager.setOffscreenPageLimit(offscreenPageLimit);
     }
 
+    @ReactProp(name = "initialPage")
+    public void setInitialPage(ReactViewPager pager, int initialPage) {
+        pager.setInitialPage(initialPage);
+    }
+
 }
diff --git a/node_modules/react-native-pager-view/src/ViewPager.tsx b/node_modules/react-native-pager-view/src/ViewPager.tsx
index 5a413b3..a118a66 100644
--- a/node_modules/react-native-pager-view/src/ViewPager.tsx
+++ b/node_modules/react-native-pager-view/src/ViewPager.tsx
@@ -57,15 +57,6 @@ export class ViewPager extends React.Component<ViewPagerProps> {
   private isScrolling = false;
   private viewPager = React.createRef<typeof ViewpagerViewManager>();
 
-  componentDidMount() {
-    // On iOS we do it directly on the native side
-    if (Platform.OS === 'android') {
-      if (this.props.initialPage != null) {
-        this.setPageWithoutAnimation(this.props.initialPage);
-      }
-    }
-  }
-
   public getInnerViewNode = (): ReactElement => {
     return this.viewPager.current!.getInnerViewNode();
   };
Read more comments on GitHub >

github_iconTop Results From Across the Web

The React Native app installed from apk stuck after initial page
This issue occur because you API not working. Solution: First: Check your API Links It must be start with https not with http....
Read more >
SDK 46 ( 47 ) Hermes is not enabled - Expo Forums
Android / real device. Hi. I want to know why i cant debug JS on chrome after upgrading SDK to 47 version ?...
Read more >
Changelog - WatermelonDB documentation - GitHub Pages
Now, _changed is not populated during initial creation of a record, but a later update will add changed fields to _changed. This change...
Read more >
Big Cheese App | Douglas Isle Of Man - Facebook
Big Cheese App, Douglas, Isle of Man. 8 likes. The official Facebook page of Big Cheese App.
Read more >
kernel/msm - Git at Google
It's sole function is to release DMA/memory resources, now that they are no longer actively being used. </para> + <para> + ->host_stop() is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found