屏幕未在本机反应中导航

screens are not navigating in react native

我有一个登录屏幕,然后从那里导航到主屏幕,在主屏幕上有 4 个按钮字母,Ch-1、Ch-2 和 Ch-3,通过按这些按钮中的每一个,我可以轻松导航到正确的屏幕,但在字母屏幕内,我还有 2 个按钮学习和进行测试但是当我按下这些按钮时,没有任何反应,我无法导航到相应的屏幕而且没有错误。我猜它的嵌套导航。 对于那里的专家来说,我可能听起来很愚蠢 :) 但我是新手(从头开始)对本机和导航的东西做出反应我真的不知道该怎么做!如有帮助将不胜感激!

代码如下:

这是我的 home.js(在文件夹屏幕中):

import React from "react";
import {View, StyleSheet, Text} from 'react-native';
import { TouchableOpacity } from "react-native-gesture-handler";
import { Auth } from "../services";

export default home =({navigation}) => {
    return (
        <View style={styles.body}>
            <Text style={styles.toptext}>
                Merheba!
            </Text>

        

            <TouchableOpacity onPress={() => navigation.navigate('alphabets')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Alphabets</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={() => navigation.navigate('ch1')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Chapter 1</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={() => navigation.navigate('ch2')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Chapter 2</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={() => navigation.navigate('ch3')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Chapter 3</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={() => Auth.signOut()}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Sign Out</Text>
            </TouchableOpacity>

        </View>
    )
}

这是来自 screens 文件夹的index.js:

export {default as home} from './home';

export {default as login} from './login';
export {default as forgotp} from './login';
export {default as signup} from './signup';
export {default as alphabets} from './alphabets';
export {default as alphl} from './alphl';
export {default as alpht} from './alpht';
export {default as ch1} from './ch1';
export {default as ch2} from './ch2';
export {default as ch3} from './ch3';

这是来自 screens 文件夹的alphabets.js:

import { NavigationContainer } from "@react-navigation/native";
import React from "react";
import {View, StyleSheet, Text} from 'react-native';
import { TouchableOpacity } from "react-native-gesture-handler";


export default alphabets =({navigation}) => {
    return (

       

        <View style={styles.body}>
       

            <Text style={styles.toptext}>
                Merheba!
            </Text>

            <TouchableOpacity onPress={() => navigation.navigate('alphl')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Learning Sheet</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={() => navigation.navigate('alpht')}
            style={styles.button}
            >
                <Text style={styles.buttontext}>Give Test</Text>
            </TouchableOpacity>


        </View>
    )
}

AppNavigation.js 来自导航文件夹:

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import home from '../screens/home';
import alphabets from '../screens/alphabets';
import ch1 from '../screens/ch1';
import ch2 from '../screens/ch2';
import ch3 from '../screens/ch3';



const stack=createStackNavigator();

export default AppNavigator =  () => {
    return(
    <stack.Navigator
        screenOptions={{
            headerShown: null
        }}
    >
        <stack.Screen name="home" component={home} />
        <stack.Screen name="alphabets" component={alphabets} />
        <stack.Screen name="ch1" component={ch1}/>
        <stack.Screen name="ch2" component={ch2} />
        <stack.Screen name="ch3" component={ch3} />
        

    </stack.Navigator>
       
    )  
}

AlphNavigator.js 来自导航文件夹:

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import alphl from '../screens/alphl';
import alpht from '../screens/alpht';

const stack=createStackNavigator();
 
export default AlphNavigator = () => {
    return(
    <stack.Navigator
        screenOptions={{
            headerShown: null
        }}
    >
        <stack.Screen name="alphl" component={alphl} />
        <stack.Screen name="alpht" component={alpht} />
       
        

    </stack.Navigator>
       
    )  
}

& 最后 index.js 来自导航文件夹:

import React, {useState, useEffect} from 'react';
import { NavigationContainer } from '@react-navigation/native';
import  AppNavigator  from './AppNavigator';
import AuthNavigator  from './AuthNavigator';
import auth from '@react-native-firebase/auth';

export default AppContainer = () => {

     // Set an initializing state whilst Firebase connects
  const [initializing, setInitializing] = useState(true);
  const [user, setUser] = useState();

  // Handle user state changes
  function onAuthStateChanged(user) {
    setUser(user);
    if (initializing) setInitializing(false);
  }

  useEffect(() => {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber; // unsubscribe on unmount
  }, []);
  if (initializing) return null;


  return(
      <NavigationContainer>
          {user ? <AppNavigator/>  : <AuthNavigator/>}
      </NavigationContainer>


      
  )
}

您没有在“AppNavigator”中定义“AlphNavigator”。

您应该在 AppNavigator 中执行此操作:

export default AppNavigator =  () => {
return(
<stack.Navigator
    screenOptions={{
        headerShown: null
    }}
>
    <stack.Screen name="home" component={home} />
    <stack.Screen name="alphabets" component={alphabets} />
    <stack.Screen name="alph" component={ AlphNavigator } /> /*here add your AlpNavigator*/
    <stack.Screen name="ch1" component={ch1}/>
    <stack.Screen name="ch2" component={ch2} />
    <stack.Screen name="ch3" component={ch3} />

</stack.Navigator>
)}

在这种情况下您没有任何错误是正常的,因为如果屏幕不存在,React Navigation 不会将您标记为错误。

如果您使用的是 TypeScript,上述问题就可以解决。

所以现在,为了访问嵌套导航屏幕,您应该使用:

navigation.navigate('alph', { screen: 'alphl' })

第一个参数是堆栈导航的名称,第二个参数是此嵌套导航中屏幕名称的对象。