【问题标题】:How to solve error in ReactNative react-navigation?如何解决 ReactNative 反应导航中的错误?
【发布时间】:2021-01-10 09:37:22
【问题描述】:

我不断收到此错误消息,尤其是当我将钩子应用于会话管理的条件时。

找不到导航对象。您的组件是否在导航器的屏幕内?

你能帮我解决这个问题吗?

这是我的代码。我不知道我哪里做错了。

Navigation.jsx

const Navigation = () => {
  const { user, isLoading } = useHook();
        if (isLoading) {
        return <SplashScreen />;
    }
    return (
        <>
        <StatusBar barStyle="default" />
            <Stack.Navigator>
                {user ? (
                    <>
            <Stack.Screen name={routes.Deliveries} 
            options={{
              headerShown: false,
            }}>
              {() => <MainTab />}
            </Stack.Screen>
                </>
                ) : (
                <>
                    <Stack.Screen name={routes.Login} component={LoginScreen} 
          options={{
            headerShown: false,
          }}/>
          </>
                )}
            </Stack.Navigator>
        </>
    );
}

export default Navigation;

App.js

import React, { createContext } from 'react';
import { StyleSheet, SafeAreaView, } from 'react-native';
import { ThemeProvider } from 'styled-components';
import theme from 'utils/ThemeStyle';
import Navigation from './src/components/navigation';
import { NavigationContainer } from '@react-navigation/native';
import GlobalHook from './src/globalHooks';

export const GlobalContext = createContext({});


export default function App() {
    const globalData = GlobalHook();
    return (
        <SafeAreaView style={styles.container}>
            <NavigationContainer>
                <GlobalContext.Provider value={globalData}>
                    <ThemeProvider theme={theme}>
                        <Navigation />
                    </ThemeProvider>
                </GlobalContext.Provider>
            </NavigationContainer>
        </SafeAreaView>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});

错误在我声明我的钩子对象后立即开始。

【问题讨论】:

    标签: javascript react-native expo react-navigation


    【解决方案1】:

    尝试使用 NavigationContainer 来包装您的应用程序

    import { NavigationContainer } from '@react-navigation/native';
    const Navigation = () => {
      const { user, isLoading } = useHook();
            if (isLoading) {
            return <SplashScreen />;
        }
        return (
            <>
            <StatusBar barStyle="default" />
              <NavigationContainer>
                <Stack.Navigator>
                    {user ? (
                        <>
                <Stack.Screen name={routes.Deliveries} 
                options={{
                  headerShown: false,
                }}>
                  {() => <MainTab />}
                </Stack.Screen>
                    </>
                    ) : (
                    <>
                        <Stack.Screen name={routes.Login} component={LoginScreen} 
              options={{
                headerShown: false,
              }}/>
              </>
                    )}
                </Stack.Navigator>
               </NavigationContainer>
            </>
        );
    }
    
    export default Navigation;
    
    

    如果没有安装

    yarn add @react-navigation/native
    

    【讨论】:

    • 我已经安装了它并在我的 app.tsx 中调用了
    • 你能分享你的 app.tsx
    • 完成。我编辑了我的问题并将其包含在其中
    • 这解决了。感谢您有兴趣提供帮助
    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2020-02-03
    • 2021-12-06
    • 2020-12-16
    • 2021-10-01
    相关资源
    最近更新 更多