Dim*_*iwa 8 javascript reactjs react-native react-navigation react-context
当前行为
我有一个使用react-navigation
v5 进行路由的本机应用程序。
由于(1),我的结构是drawerNavigator (a) > stackNavigator (b) > views (c)
.
当我尝试useNavigation()
在 my 中调用钩子时<DrawerContent />
,出现以下错误:
Error: We couldn't find a navigation object. Is your component inside a navigator?
at useNavigation (bundle.js:8766)
Run Code Online (Sandbox Code Playgroud)
是的,我不在里面,stackNavigator
所以无法调用钩子
预期行为
我希望在我的<DrawerContent />
.
您的环境
| software | version |
| ------------------------------ | ------- |
| iOS or Android | iOS, Android and web
| @react-navigation/native | 5.0.0-alpha.41
| @react-navigation/stack | 5.0.0-alpha.63
| @react-navigation/drawer | 5.0.0-alpha.41
| react-native-reanimated | 1.4.0
| react-native-gesture-handler | 1.5.3
| react-native-safe-area-context | 0.6.2
| react-native-screens | 2.0.0-alpha.32
| react-native | https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz
| expo | SDK36
| node | v13.5.0
| npm or yarn | 6.13.7
Run Code Online (Sandbox Code Playgroud)
我如何使用@react-navigation/stack
inside@react-navigation/drawer
或者我应该如何使用它们构建我的抽屉和应用程序?
Dim*_*iwa 12
这可以使用https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
创建RootNavigation
:
import { createRef } from 'react';
export const navigationRef = createRef();
export function navigate(name, params) {
navigationRef.current?.navigate(name, params);
}
export function goBack() {
navigationRef.current?.goBack();
}
Run Code Online (Sandbox Code Playgroud)
在App.js
:
import { navigationRef } from './navigation/RootNavigation';
// ...
<NavigationContainer
ref={navigationRef}
>
{children}
</NavigationContainer>
Run Code Online (Sandbox Code Playgroud)
然后RootNavigation
从任何来源导入,您将不必担心反应树。