How can i access to redux store with react navigation?

前端 未结 2 1949
小鲜肉
小鲜肉 2021-01-29 11:00

I have App \"Music App\" for tow users type \"Guest, a user registered\"

I have a bottom navigator for those, When Guest opens my app I want to render just 4 bottom tabs

2条回答
  •  北海茫月
    2021-01-29 11:58

    You need to create dynamic routes, and one component that renders tabs based on whether the user is logged in or not. in Tabs.js do the following.

    const loginRoutes = {
      Home: {
        screen: Home,
        navigationOptions: {
          title: 'Home',
        },
      },
      Browse: {
        screen: Browse,
        navigationOptions: {
          title: 'Browse',
        },
      },
      Search: {
        screen: Search,
        navigationOptions: {
          title: 'Search',
        },
      },
      Radio: {
        screen: Radio,
        navigationOptions: {
          title: 'Radio',
        },
      },
      Library: {
        screen: Library,
        navigationOptions: {
          title: 'Library',
        },
      },
    }
    
    const noLoginRoutes = {
      Home: {
        screen: Home,
        navigationOptions: {
          title: 'Home',
        },
      },
      Browse: {
        screen: Browse,
        navigationOptions: {
          title: 'Browse',
        },
      },
      Search: {
        screen: Search,
        navigationOptions: {
          title: 'Search',
        },
      },
      Radio: {
        screen: Radio,
        navigationOptions: {
          title: 'Radio',
        },
      }
    }
    
    const mapStateToProps = state => {
      return {
        isLogin: state.user.isLogin,
     };
    };
    
    
     const AppNav = ({ isLogin }) => {
      const Container = createAppContainer(
        createDrawerNavigator(
          {
            ...drawerRoutes,
            App: createStackNavigator(
              {
                ...routes,
                Tabs: createBottomTabNavigator(
                  isLogin ? loginRoutes : noLoginRoutes
                ),
              },
              routesConfig
            ),
          },
          drawerConfig
        )
      );
    
      return ;
    };
    
    export default connect(mapStateToProps)(AppNav);
    

    DEMO

提交回复
热议问题