Combining React-Native-Paper BottomNavigation with StackNavigator

1.5k views Asked by At

I am currently using React-Native-Paper bottomNavigation. It is working great. However, the next requirement was to add Stack Navigator to individual screens. I have this working on regular bottom-Bar navigation but not able to get it working with React-Native-Paper library.

const SubjectNavigator = createStackNavigator({
  Subjects: SubjectScreen,
  Topics: TopicListScreen
}, {
    navigationOptions: {}
});

const NavigationController = () => {
  const [index, setIndex] = React.useState(0);
  const [routes] = React.useState([
    { key: 'home', title: 'Home', icon: 'home', color: '#3F51B5' },
    { key: 'subjects', title: 'Subjects', icon: 'book-open', color: '#009688' },
    { key: 'tournaments', title: 'Tournaments', icon: 'trophy', color: '#795548' },
    { key: 'videos', title: 'Video Feeds', icon: 'video', color: '#607D8B' }
  ]);

  const renderScene = BottomNavigation.SceneMap({
    home: HomeScreen,
    subjects: SubjectScreen,
    tournaments: TournamentScreen,
    videos: VideoScreen
  });

  return (
    <BottomNavigation
      navigationState={{ index, routes }}
      onIndexChange={setIndex}
      renderScene={renderScene}
    />
  );
};

export default NavigationController;

Doing this would give me an error like ---- Can't Find Variable: navigation when trying to move from one screen to another in the subjectNavigator stack.

Please HELP!!

1

There are 1 answers

0
tiller1010 On

You could create a stack navigator component that will be used for each of your routes. Then, pass in an initialRouteName prop.

class AppStackScreen extends React.Component {

    render(){
        return(
            <NavigationContainer>
                <Stack.Navigator initialRouteName={this.props.initialRouteName}>
                  <Stack.Screen
                    name="Home"
                    component={HomeScreen}
                  />
                  <Stack.Screen
                    name="Subjects"
                    component={SubjectScreen}
                  />
            </NavigationContainer>
        );
    }
}

Then in your BottomNavigation

<BottomNavigation
    navigationState={this.state}
    onIndexChange={this.handleIndexChange}
    renderScene = {({ route, jumpTo }) => {
        switch (route.key) {
            case 'Home':
                return <AppStackScreen initialRouteName="Home" jumpTo={jumpTo} />;
            break;
            case 'Subjects':
                return <AppStackScreen initialRouteName="Subjects" jumpTo={jumpTo} />;
            break;