Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First tab is always rendered when onSwitchToTab and a custom currentTabIndex is set #7921

Closed
1 task done
ismarbesic opened this issue Oct 11, 2024 · 0 comments · Fixed by #7922
Closed
1 task done

Comments

@ismarbesic
Copy link
Contributor

ismarbesic commented Oct 11, 2024

What happened?

We have noticed that when using an application with bottom tabs with multiple tabs and the "tabsAttachMode" option is set to "onSwitchToTab" and setting "currentTabIndex" to any value higher than 0 (e.g. 1), the first tab's content will always be rendered together with the content of the second tab. This behavior only occurs when the "currentTabIndex" is not set to its default value. In a test application running 7.39.2 (see code in the repro section), we can see the following in the console:

BUNDLE  ./index.js 

LOG  Running "Home" with {"rootTag":1,"initialProps":{"componentId":"Component1"}}
LOG  render home
LOG  Running "Settings" with {"rootTag":11,"initialProps":{"componentId":"Component2"}}
LOG  render settings

Upon debugging I inspected the RNNBottomTabsController class, placing a breakpoint at -[RNNBottomTabsController selectedViewController], and noticed that this method is called before _currentTabIndex is properly initialized, which defaults to 0. This means the renderer will always render the content of the first tab, before the rest of the execution happens which sets the selected tab to index 1.

One solution to fix this issue is to properly initialize the _currentTabIndex value before the super() method is called.

Screenshot 2024-10-11 at 10 21 46

What was the expected behaviour?

We expect only one screen being rendered:

BUNDLE  ./index.js 

LOG  Running "Settings" with {"rootTag":21,"initialProps":{"componentId":"Component2"}}
LOG  render settings

Was it tested on latest react-native-navigation?

  • I have tested this issue on the latest react-native-navigation release and it still reproduces.

Help us reproduce this issue!

Repro (App.tsx):

import {Navigation} from 'react-native-navigation';

const HomeScreen = () => {
  console.log('render home');
  return null;
};

HomeScreen.options = {
  topBar: {
    title: {
      text: 'Home',
    },
  },
  bottomTab: {
    text: 'Home',
  },
};

const SettingsScreen = () => {
  console.log('render settings');
  return null;
};

SettingsScreen.options = {
  topBar: {
    title: {
      text: 'Settings',
    },
  },
  bottomTab: {
    text: 'Settings',
  },
};

Navigation.registerComponent('Home', () => HomeScreen);
Navigation.registerComponent('Settings', () => SettingsScreen);

Navigation.events().registerAppLaunchedListener(() => {
  Navigation.setRoot({
    root: {
      bottomTabs: {
        id: 'bottomBar',
        options: {
          bottomTabs: {
            tabsAttachMode: 'onSwitchToTab',
            currentTabIndex: 1,
          },
        },
        children: [
          {stack: {children: [{component: {name: 'Home'}}]}},
          {stack: {children: [{component: {name: 'Settings'}}]}},
        ],
      },
    },
  });
});

In what environment did this happen?

React Native Navigation version: 7.39.2
React Native version: 0.74.1
Has Fabric (React Native's new rendering system) enabled: No
Node version: 18.18.2
Device model: iPhone 16 Pro
iOS version: 18.0

@ismarbesic ismarbesic changed the title First tab is always being rendered when onSwitchToTab and a custom currentTabIndex is set First tab is always rendered when onSwitchToTab and a custom currentTabIndex is set Oct 11, 2024
asafkorem added a commit that referenced this issue Oct 16, 2024
This fixes the issue mentioned on #7921, #7922 and #7924.

Changing the `selectedIndex` will not have any effect before the setup has finished,
 the original issue was the method `setSelectedIndex` was called immediately when calling
 to the `super` initializer with a `0` value regardless of the initial `currentTab`.

This prevents the loading of the tab from index `0` regardless of the custom initial rendered tab.
asafkorem added a commit that referenced this issue Oct 16, 2024
)

This fixes the issue mentioned on #7921, #7922 and #7924.

Changing the `selectedIndex` will not have any effect before the setup has finished,
 the original issue was the method `setSelectedIndex` was called immediately when calling
 to the `super` initializer with a `0` value regardless of the initial `currentTab`.

This prevents the loading of the tab from index `0` regardless of the custom initial rendered tab.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant