React Native - Accessing drawer navigation outside of AppNavigator

故事扮演 提交于 2020-01-16 09:08:44

问题


  App.js 
    <Store>
      <Navbar />
      <AppNavigator ref={navigatorRef => {
          NavigationService.setTopLevelNavigator(navigatorRef);
        }} />
    </Store> 

I wanna be able to access

 props.navigation.openDrawer();

from navbar but I get

undefined is not an object (evaluating 'props.navigation.openDrawer')

onPress
    Navbar.js:70:29
    etc..

How can I allow NavBar to access the drawer?


回答1:


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()

don't forget to make respective imports



来源:https://stackoverflow.com/questions/58563204/react-native-accessing-drawer-navigation-outside-of-appnavigator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!