简体   繁体   English

反应按选定页面导出本机导航

[英]React Native Navigation Export by Selected Page

First I have defined a createStackNavigator on the APP.js page. 首先,我在APP.js页面上定义了createStackNavigator。

const MainNavigator=createStackNavigator({
 Home: {
  screen: HomePage,
  navigationOptions:{
  header:null }},
SignUpPages:{
  screen:SignUpPages,
  navigationOptions:{
  header:null } },
ProfilePages: {screen: ProfilePages,navigationOptions:{title:'Test Project',headerLeft:null,headerStyle: { backgroundColor: '#004f9e'},headerTitleStyle: { color: 'white',textAlign: 'center',flex: 1}}}});

I then define createBottomTabNavigator for other pages. 然后,我为其他页面定义createBottomTabNavigator。

const BottomNavigator=createBottomTabNavigator({
      ProfilePages: {
      screen: ProfilePages,
      navigationOptions:{
      title:'Profile Pages',
      headerLeft:null,
      headerStyle: { backgroundColor: '#004f9e'},
      headerTitleStyle: { color: 'white',textAlign: 'center',flex: 1, },
      }
  },

  PlatePages: {
      screen: PlatePages,
      navigationOptions:{
      title:'Hafriyat Plate Pages',
      headerLeft:null,
      headerStyle: { backgroundColor: '#004f9e'},
      headerTitleStyle: { color: 'white',textAlign: 'center',flex: 1, },
      }
  },

  QueryPages: {
      screen: QueryPages,
      navigationOptions:{
      title:'Query Pages',
      headerLeft:null,
      headerStyle: { backgroundColor: '#004f9e'},
      headerTitleStyle: { color: 'white',textAlign: 'center',flex: 1, },
      }
  }
})

When the first project is opened, the login page should be opened and the sign-up page can be accessed. 当打开第一个项目时,应打开登录页面并可以访问注册页面。 If logged in, go to the profile page and createBottomTabNavigator should be opened on this page. 如果已登录,请转到配置文件页面,并应在此页面上打开createBottomTabNavigator。

I can only export one navigator from one page. 我只能从一页导出一个导航器。 From this page, I thought to export the data with this.props.state and then export the data according to the data. 从这个页面,我想使用this.props.state导出数据,然后根据该数据导出数据。 How can I do that? 我怎样才能做到这一点?

const App=createAppContainer(MainNavigator);
const App2=createAppContainer(BottomNavigator)

if(this.props.valMenu=='Main'){
export default App

}
else if(this.props.valMenu=='Bottom'){
export default App2
}

I would propose something different, not sure if it's the correct way to do it but it works for me. 我会提出一些不同的建议,不确定这样做是否正确,但对我有用。

I'd recommend you add a function to get if the user is logged on or not and then based on that you can route the user to the different screens that you have. 我建议您添加一个功能来获取用户是否登录,然后基于该功能可以将用户路由到您拥有的不同屏幕。 From the code snippet that you gave, it would go something like this: 从您提供的代码段中,它将类似于以下内容:

const NotLoggedNavigator=createStackNavigator({
  SignUpPages:{
    screen:SignUpPages,
    navigationOptions:{
      header:null 
    } 
  }
});

const BottomNavigator=createBottomTabNavigator({
  ...
});

const MainNavigator=createStackNavigator({
  Home: { screen: HomePage },
  NotLoggedStack: { screen: NotLoggedNavigator },
  LoggedStack: { screen: BottomNavigator },
}, {
  initialRouteName: 'Home',
  headerMode: 'null',
});

const AppContainer = createAppContainer(MainNavigator);

Doing this you would export only one appContainer. 这样做只会导出一个appContainer。 Then when your app starts you'd check if the user if logged on or not and route accordingly, something like this: 然后,当您的应用启动时,您将检查用户是否已登录并进行相应的路由,如下所示:

if (user.isSignedUp) {
  navigation.dispatch(StackActions.reset(
    { index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'NotLoggedStack' })] }
  ))
} else {
  navigation.dispatch(StackActions.reset(
    { index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedStack' })] }
  ))
}

I did not test the code, but I think it's a clean way to route to different stacks if user is logged on or not, hope it helps. 我没有测试代码,但是我认为这是一种不管用户是否登录都可以路由到不同堆栈的干净方法,希望它能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM