javascript - 如何解决 ReactNative 反应导航中的错误?
问题描述
我不断收到此错误消息,尤其是当我将钩子应用于会话管理的条件时。
找不到导航对象。您的组件是否在导航器的屏幕内?
你能帮我解决这个问题吗?
这是我的代码。我不知道我在哪里做错了。
导航.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;
应用程序.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,
},
});
错误在我声明我的钩子对象后立即开始。
解决方案
尝试像这样使用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
推荐阅读
- javascript - 为 iframe 中的每个元素重放滚动事件
- java - 如果我暂停计时器,我会在计时器中遇到问题,它不会从停止的地方开始
- c++ - Visual Studio 2019 无法正确处理动态结构数组的聚合初始化
- python-3.x - Python/Pandas - ValueError:传递值的形状
- angular - 如何使用 Observable 在 Angular8 中过滤具有多个条件的数组?
- javascript - 即使组件已安装,也未安装 setState 警告
- javascript - 复选框不会附加
- javascript - 单击jQuery中的按钮时,是否有解决Asp.Net gridview行数据传输到某些特定文本框的解决方案?
- python - 在 exec 节点中传递有效负载值
- php - Symfony 4中用户密码的自定义表单