Search code examples
react-nativetabbarnavigator

how to use tabbar in navigator in react-native


I am now having trouble about this issue.

Because there are some screens which does not use tabbar and also I need to use vector icon in navigationbar, instead of using react-native-tabbar-navigator, I tried to use tabbar in Navigator as below.

render() {
  return (
      <Navigator
        initialRoute={{name: 'LearningList', index: 0}}
        renderScene={(route, navigator) =>
          {
            if (route.name == 'LearningList') {
              return (
                <LearningList navigator={navigator} />
              );
            }
            if (route.name == 'MyLearning') {
              return (
                <View style={{ flex: 1, }}>
                  <MyLearning navigator={navigator} />
                  <TabBarIOS
                    tintColor="black"
                    barTintColor="#3abeff">
                    <Ionicon.TabBarItemIOS
                      style={ styles.tabBarItem }
                      selected={false}
                      iconName='ios-search'
                      title='Explorer'
                      navigator={navigator}
                      onPress={ this.changeTabSelection('LearningList') }>
                      <View></View>
                    </Ionicon.TabBarItemIOS>
                    <Ionicon.TabBarItemIOS
                      style={{ backgroundColor: 'green' }}
                      selected={true}
                      iconName='ios-list-outline'
                      title='My Learning'
                      navigator={navigator}
                      onPress = { this.changeTabSelection('MyLearning') }>
                      <View></View>
                    </Ionicon.TabBarItemIOS>
                  </TabBarIOS>
                </View>
              );
            }
            if (route.name == 'Schedule') {
              return (
                <Schedule navigator={navigator} learningID={ route.learningID } />
              );
            }            
          }
        }
      />
  );
}

But when I click TabBarItemIOS buttons, onPress event does not invoked at all, and if I click edit button in LearningList page, the onPress callbacks are invoked for all TabBarItemIOS buttons.

Here is LearningList.js next button content.

const rightButtonConfig = {
   title: 'Next >',
   handler: () => {
    this.props.navigator.push({
     name: 'MyLearning'
    });
   }
  };

So, I hope to know the right way of using tabbar in Navigator.

Please help me!!!


Solution

  • It is because you are calling that function instead of passing a function reference. To do what you want, you simply need arrow functions. For example replace:

    onPress={ this.changeTabSelection('LearningList') }>
    

    to

    onPress={ () => this.changeTabSelection('LearningList') }>