简体   繁体   English

如何在三元渲染中从一个屏幕导航到另一个屏幕

[英]How navigate from a screen to another in ternary render

I got the following react-navigation structure:我得到了以下 react-navigation 结构:

<NavigationContainer>
      {authToken ? (
        <Tab.Navigator>
          <Tab.Screen
            name='HomeScreen'
            component={HomeScreen}
          />
          <Tab.Screen
            name='SettingsScreen'
            component={SettingsScreen}
          />
        </Tab.Navigator>
      ) : (
        <Stack.Navigator>
          <Stack.Screen name="SignUpScreen" component={SignUpScreen} />
        </Stack.Navigator>
      )}
    </NavigationContainer>

How to navigate from SignUpScreen to HomeScreen and conversely:如何从 SignUpScreen 导航到 HomeScreen,反之亦然:

// HomeScreen.js button :

onPress={() => {
  // remove authToken and then :
  navigation.navigate(`SignUpScreen`);
}}

Got the error message: The action 'NAVIGATE' with payload {"name":"SignUpScreen"} was not handled by any navigator. Do you have a screen named 'SignUpScreen'?收到错误消息: The action 'NAVIGATE' with payload {"name":"SignUpScreen"} was not handled by any navigator. Do you have a screen named 'SignUpScreen'? The action 'NAVIGATE' with payload {"name":"SignUpScreen"} was not handled by any navigator. Do you have a screen named 'SignUpScreen'?

Same from SignUpScreen after authToken = true .authToken = true之后与SignUpScreen相同。

Is there a way to navigate from one stack to another in a ternary operator?有没有办法在三元运算符中从一个堆栈导航到另一个堆栈? (Or trigger the index of the navigation to re-render the condition?). (或者触发导航的索引重新渲染条件?)。

Assuming what what you want to implement is an App that has a different navigation if you are logged in or out, I would suggest using the Switch navigator.假设您要实现的是一个在您登录或注销时具有不同导航的应用程序,我建议使用 Switch 导航器。 In your case, your error it is most likely caused because the signup screen does not exist in the tree of both stack navigators.在您的情况下,您的错误很可能是因为两个堆栈导航器的树中都不存在注册屏幕。 I'll post the entire main code but the part you should take a look is this:我将发布整个主要代码,但你应该看看的部分是:

const Navigator = createSwitchNavigator({
        Main: MainTabNavigator,
        Auth: AuthNavigator,
        SplashScreen: SplashScreen,
        Onboarding: Onboarding,
      }, {
        initialRouteName: 'SplashScreen',
        headerMode: 'none'
    });

The rest: rest:

import React from "react";
import { createStackNavigator, createBottomTabNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';
import {ThemeProvider} from 'styled-components'
import {configureStore} from "./redux/store"
import { Provider } from 'react-redux'

const theme = {
  primaryColor: '#3F51B5',
  darkPrimaryColor: '#303F9F',
  containerBackgroundColor: '#FFFFFF',
  lightPrimaryColor: '#C5CAE9',
  accentColor: '#FF5722',
  backgroundColor: 'white',
  font: 'Lato-Regular',
  fontBold: 'Lato-Bold'
}

const ConfigNavigator = createStackNavigator({
  Config: ConfigScreen,
  SettingsScreen: SettingsScreen,
  ...coreRoutes
}, {
  initialRouteName: 'Config',
  headerMode: 'none'
});

const HomeNavigator = createStackNavigator({
  ...coreRoutes
}, {
  initialRouteName: 'HomeScreen',
  headerMode: 'none'
});

const PlacesNavigator = createStackNavigator({
  Places: PlacesScreen,
  POI: POIScreen,
}, {
  initialRouteName: 'Places',
  headerMode: 'none'
});

const PinnedNavigator = createStackNavigator({
  Pinned: PinnedScreen,
  POI: POIScreen,
}, {
  initialRouteName: 'Pinned',
  headerMode: 'none'
});

const MainTabNavigator = createBottomTabNavigator({
  Home: HomeNavigator,
  Places: PlacesNavigator,
  Pinned: PinnedNavigator,
  Chat: ChatScreen,
  Config: ConfigNavigator,
}, {
  initialRouteName: 'Home',
  tabBarPosition: "bottom",
  swipeEnabled: false,
  tabBarComponent: props => {
    return <TabNavigation {...props}
    items={[
      {
        text: Strings['home'], icon: { name: "home", type: "MaterialCommunityIcons" },
        route: "Home"
      },
      {
        text: Strings['places'], icon: { name: "map-marker-multiple", type: "MaterialCommunityIcons"},
        route: "Places"
      },
      {
        text: Strings['pinned'], icon: { name: "pin", type: "MaterialCommunityIcons" },
        route: "Pinned"
      },
      {
        text: Strings['chat'], icon: { name: "wechat", type: "MaterialCommunityIcons" },
        route: "Chat"
      },
      {
        text: Strings['settings'], icon: { name: "settings", type: "MaterialCommunityIcons" },
        route: "Config"
      }
    ]}
    />
  }
});

const AuthNavigator = createStackNavigator({
  Registration: RegistrationScreen,
}, {
  initialRouteName: 'Registration',
  headerMode: 'none'
});

const Navigator = createSwitchNavigator({
    Main: MainTabNavigator,
    Auth: AuthNavigator,
    SplashScreen: SplashScreen,
    Onboarding: Onboarding,
  }, {
    initialRouteName: 'SplashScreen',
    headerMode: 'none'
});

const AppContainer = createAppContainer(Navigator)

let store = configureStore();

class App extends React.Component {

  constructor(){
      super();
      this.theme = new Theme(theme);
  }

  render(){
    return <Provider store={store}>
              <ThemeProvider theme={this.theme.getTheme()}>
                  <AppContainer/>
              </ThemeProvider>
          </Provider>
  }
};

export default App;

From then, you just navigate to Auth or Main and the entire navigator tree changes.从那时起,您只需导航到 Auth 或 Main,整个导航器树就会发生变化。

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

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