Dynamically change drawerPosition config of DrawerNavigator in react-navigation

拜拜、爱过 提交于 2019-12-07 12:15:17

问题


I have a DrawerNavigator config for my main routes

const App = DrawerNavigator(DrawerRoutes, {
  initialRouteName: 'Main',
  contentComponent: ({ navigation }) => <DrawerMenu navigation={navigation} routes={DrawerRoutes} />,
  mode: Platform.OS === 'ios' ? 'modal' : 'card',
  drawerWidth: Dimensions.get('window').width - (Platform.OS === 'android' ? 56 : 64),
  drawerPosition: 'left',
})

I want to change the drawerPosition property depending on the current language of the application. I have already tried to connect to redux with no success because it throws an error for invalid React element. How can I change the drawerNavigator properties on the fly?


回答1:


I ended up with a not so elegant solution.

I copied the DrawerNavigator of react-navigation and added it to my project.

The imports that weren't working properly were fixed to point to the original react-navigation project and I implemented the application logic on the drawerPosition property on the DrawerView component

 const Drawer = I18nReader()(
({ i18n, ...restProps }) =>
  <DrawerView
    {...restProps}
    useNativeAnimations={useNativeAnimations}
    drawerWidth={drawerWidth}
    contentComponent={contentComponent}
    contentOptions={contentOptions}
    drawerPosition={i18n.rtl ? 'right' : 'left'}
  />
)

const navigator = createNavigator(
  drawerRouter,
  routeConfigs,
  config,
  NavigatorTypes.DRAWER
)(Drawer)


来源:https://stackoverflow.com/questions/46303188/dynamically-change-drawerposition-config-of-drawernavigator-in-react-navigation

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