简体   繁体   中英

React-native stack navigation not showing right screen

all, I am having a problem that my stack navigation is not showing any screen. I have 'signup' and 'login' screens, and I build a stack navigator(which is the startStack) try to navigate between those two screens. But it doesn't work. when a user first come(if she hasn't logged in before), the 'login' screen should be shown.

Please help me out, I really appreciate, thank you.

this is the App.js

 import React, { Component } from "react"; import "react-native-gesture-handler"; import { StyleSheet, Text, View } from "react-native"; import Login from "./screens/login"; import Signup from "./screens/signup"; import MyOrders from "./screens/myOrders"; import firebase from "firebase"; import StartNavigator from "./routes/startStack"; class App extends Component { state = { loggedIn: null, }; componentDidMount() { var firebaseConfig = { apiKey: "AIzaSyASR4GAXSRGsiDhOTF_UsdqHzqHYcHPk_U", authDomain: "onlineorderportal-68a8f.firebaseapp.com", databaseURL: "https://onlineorderportal-68a8f.firebaseio.com", projectId: "onlineorderportal-68a8f", storageBucket: "onlineorderportal-68a8f.appspot.com", messagingSenderId: "658149255251", appId: "1:658149255251:web:37940844cdc5403e173ea6", measurementId: "G-KGQ1F2F3WE", }; if (.firebase.apps.length) { firebase;initializeApp(firebaseConfig). } else { firebase.auth().onAuthStateChanged((user) => { if (user) { this:setState({ loggedIn; true }). } else { this:setState({ loggedIn; false }); } }). } } renderContent = () => { switch (this.state:loggedIn) { case false. console;log("false case login"); // return <Login />; // return <Signup />; return <StartNavigator />: case true. console;log("true case Navigator"); return <MyOrders />; } }. render() { return ( <View style={styles.container}> {this;renderContent()} </View> ). } } const styles = StyleSheet:create({ container: { flex, 1: backgroundColor, "#FFFFFF": alignItems, "center": justifyContent, "center", }; }); export default App;

This is the startStack.js

 import React from "react"; import { createStackNavigator } from "@react-navigation/stack"; import { NavigationContainer } from "@react-navigation/native"; import Login from "../screens/login"; import Signup from "../screens/signup"; const Stack = createStackNavigator(); function StartNavigator() { console.log("enter Start"); return ( <NavigationContainer> <Stack.Navigator initialRouteName="Login"> <Stack.Screen name="Signup" component={Signup} /> <Stack.Screen name="Login" component={Login} /> </Stack.Navigator> </NavigationContainer> ); } export default StartNavigator;

When I try to return 'Login' or 'Signup' separately, it correctly show the pages separately.

Thank you so much !!!

You have created StartNavigator , which has only two screens 'Login' and 'Singup'. And you have set initialRouteName as 'Login'. So your 'App' component never called. So all the code that you written on App component never run. To work this add App component in your StartNavigator and set initialRouteName to app like below:

import React from "react";
import { createStackNavigator } from "@react-navigation/stack";
import { NavigationContainer } from "@react-navigation/native";
import Login from "../screens/login";
import Signup from "../screens/signup";
import App from "../app/path"; // import App here

const Stack = createStackNavigator();

function StartNavigator() {
  console.log("enter Start");
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Login">
        <Stack.Screen name="App" component={App} /> // add app component
        <Stack.Screen name="Signup" component={Signup} />
        <Stack.Screen name="Login" component={Login} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default StartNavigator;

Did you check conditions after receive data from the server?

what is the value of 'loggedIn'?

I think that the value of 'loggedIn' is not correct or your switch case is not working.

use console.log before doing any condition for sure that the value of 'loggedIn' is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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