React Native-在AppNavigator外部访问抽屉导航 [英] React Native - Accessing drawer navigation outside of AppNavigator
本文介绍了React Native-在AppNavigator外部访问抽屉导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
App.js
<Store>
<Navbar />
<AppNavigator ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}} />
</Store>
我想访问
props.navigation.openDrawer();
来自导航栏,但我得到
undefined is not an object (evaluating 'props.navigation.openDrawer')
onPress
Navbar.js:70:29
etc..
如何允许NavBar访问抽屉?
How can I allow NavBar to access the drawer?
推荐答案
我想您正在关注在没有导航道具的情况下进行导航(如果您不这样做,则应以您的情况为准).然后在NavigationService.js
中添加openDrawer方法
I suppose you are following Navigating without the navigation prop (if you don't then you should in your case). Then in NavigationService.js
add openDrawer method
// NavigationService.js
import { DrawerActions } from 'react-navigation-drawer';
...
// add this function
function openDrawer(routeName, params) {
_navigator.dispatch(DrawerActions.openDrawer());
}
export default {
...
// and export it
openDrawer
};
然后代替,使用
then instead of props.navigation.openDrawer()
, use
NavigationService.openDrawer()
别忘了分别进口
这篇关于React Native-在AppNavigator外部访问抽屉导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文