react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

Bottom Tabs tabsAttachMode onSwitchToTab does not work on iOS

Open UtillYou opened this issue 6 years ago • 11 comments

Issue Description

Bottom Tabs tabsAttachMode onSwitchToTab does not work on iOS. Android works very well. The other tabs are mounted with the home screen at the same time.

Steps to Reproduce / Code Snippets / Screenshots

import {Navigation} from 'react-native-navigation';
const React = require('react');
const { View, Text, Button, StyleSheet } = require('react-native');

const HomeScreen = (props) => {
  return (
    <View style={styles.root}>
      <Text>Hello React Native Navigation 👋</Text>

      <Button
        title='Push Settings Screen'
        color='#710ce3'
        onPress={() => Navigation.push(props.componentId, {
          component: {
            name: 'Settings'
          }
        })} />
    </View>
  );
};
HomeScreen.options = {
  topBar: {
    title: {
      text: 'Home'
    }
  },
  bottomTab: {
    text: 'Home'
  }
};

const SettingsScreen = () => {
  return (
    <View style={styles.root}>
      <Text>Settings Screen</Text>
    </View>
  );
}
SettingsScreen.options = {
  topBar: {
    title: {
      text: 'Settings'
    }
  },
  bottomTab: {
    text: 'Settings'
  }
}

const PersonScreen = () => {
  return (
    <View style={styles.root}>
      <Text>Person Screen</Text>
    </View>
  );
}
PersonScreen.options = {
  topBar: {
    title: {
      text: 'Person'
    }
  },
  bottomTab: {
    text: 'Person'
  }
}

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

Navigation.setDefaultOptions({
  statusBar: {
    backgroundColor: '#4d089a'
  },
  topBar: {
    title: {
      color: 'white'
    },
    backButton: {
      color: 'white'
    },
    background: {
      color: '#4d089a'
    }
  },
  bottomTab: {
    fontSize: 14,
    selectedFontSize: 14
  }
});

Navigation.events().registerAppLaunchedListener(async () => {
  Navigation.setRoot({
    root: {
      bottomTabs: {
        children: [
          {
            stack: {
              children: [
                {
                  component: {
                    name: 'Home'
                  }
                },
              ]
            }
          },
          {
            stack: {
              children: [
                {
                  component: {
                    name: 'Settings'
                  }
                }
              ]
            }
          },
         {
            stack: {
              children: [
                {
                  component: {
                    name: 'Person'
                  }
                }
              ]
            }
          }
        ]
      }
    }
  });
});

const styles = StyleSheet.create({
  root: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'whitesmoke'
  }
});

Environment

  • React Native Navigation version: 5.0.0
  • React Native version: 0.63.2
  • Platform(s) (iOS, Android, or both?): only iOS
  • Device info (Simulator/Device? OS version? Debug/Release?): Both Simulator and Device, iOS 13, Both Debug and Release

UtillYou avatar Sep 09 '20 09:09 UtillYou

In your example you forgot to actually add tabsAttachMode to the options. After adding

  bottomTabs: {
    tabsAttachMode: 'onSwitchToTab',
  },

It works fine on RNN v7. Feel free to reopen if this issue still occurs for you on v7.

danilobuerger avatar Sep 11 '20 18:09 danilobuerger

@danilobuerger I'm using the latest version of react-native-navigation (7.4.0) and still facing the same issue on iOS. All the screens in the root are being rendered on app start.

const MainRoot = {
  root: {
    bottomTabs: {
      id: 'stack-with-bottom',
      tabsAttachMode: 'onSwitchToTab',
      children: [
        {
          stack: {
            children: [
              {
                component: {
                  id: 'home',
                  name: 'Home',
                },
              },
            ],
          },
        },
        {
          stack: {
            children: [
              {
                component: {
                  id: 'log',
                  name: 'Logger',
                },
              },
            ],
          },
        },
      ],
    },
  },
};

export const goHome = async () => await Navigation.setRoot(MainRoot);
`

fadinasr avatar Nov 19 '20 21:11 fadinasr

@fadinasr thats not where the option belongs. Please check the docs and typings.

danilobuerger avatar Nov 19 '20 21:11 danilobuerger

@danilobuerger checked the official documents and found nothing there, any available link or idea where I can find them?

fadinasr avatar Nov 21 '20 18:11 fadinasr

@fadinasr Its best if you ask those questions on Discord. I am sure somebody will be able to help you there. We want to use the issue tracker here only for bugs and features.

danilobuerger avatar Nov 22 '20 11:11 danilobuerger

@fadinasr any solution yet?

ganesh-papola avatar Dec 17 '20 05:12 ganesh-papola

@ganesh-papola yes, you need to put it inside options like the code below

root: {
    bottomTabs: {
      id: 'stack-with-bottom',
      options: {
        bottomTabs: {
          tabsAttachMode: 'onSwitchToTab',
        },
      },
      children: [...]
    }
}

this should solve your issue 😄

fadinasr avatar Dec 17 '20 12:12 fadinasr

The tabsAttachMode option doesn't seem to do anything on my end, and I'm using it as in @fadinasr example. Also following the official typings, but still nothing. In my case tabs only get mounted on switch, and that causes a flicker on the first switch.

ernestasg152 avatar Jul 07 '22 13:07 ernestasg152

I figured out that I was wrapping the top level of my screens with SafeAreaProvider from react-native-safe-area-context, so I have removed it, and now all screens seem to be mounting. However the flicker is still there, it's just quicker now.

ernestasg152 avatar Jul 07 '22 13:07 ernestasg152

Attaching a screen recording of my case, just so it's easier to understand what I mean:

https://user-images.githubusercontent.com/36773034/177788549-602c89f0-ad75-4b2c-b737-9b94ec9e8456.mov

ernestasg152 avatar Jul 07 '22 13:07 ernestasg152

Sorry for the string of comments, I figured out what the issue on my end for this was that I moved the SafeAreaProvider to a lower level which then stopped disturbing the mounting, but flicker remained, so now I completely removed it and it works fine. So if anyone is using that, I suggest using initialWindowMetrics from that same lib which basically does the same thing for my use case, without needing the provider.

ernestasg152 avatar Jul 07 '22 17:07 ernestasg152