React navigation navigate without navigation props example not working

依然范特西╮ 提交于 2019-12-25 03:04:07

问题


I have a react-native app and I'm using the new Context API to manage the storage and stuff, now I'm wrapping the createAppContainer(AuthNavigator) in the <Provider> created by the context api, so in order to navigate from the Context API file, I tried to do the exact same example I found in the documentation by creating the NavigationService and passing the ref of the AppContainer to the NavigationService and using { NavigationActions }, somehow it's not working neither it's throwing any kind of error

here is my router.js

  const AuthNavigator = createSwitchNavigator(
    {
      Authenticated: Authenticated,
      UnAuthenticated: UnAuthenticated,
    },
    {
      initialRouteName: props.is_authenticated ? 'Authenticated' : 'UnAuthenticated'
    }
  )

  const Router = createAppContainer(AuthNavigator);

  return (

    <Provider style={{ width: '100%', height: '100%' }}>
      <Router ref={ ref => NavigationService.setTopLevelNavigator(ref) } />
      <DropdownAlert
        successColor="#26296A"
        titleStyle={{ fontFamily: 'DINNextLTArabic-Regular', lineHeight: 18, color: '#fff', textAlign: 'center' }}
        messageStyle={{ fontFamily: 'DINNextLTArabic-Regular', lineHeight: 16, color: '#fff', textAlign: 'center' }}
        imageStyle={{ display: 'none' }}
        ref={ (ref) => Flash.setDropDown(ref) }
      />
    </Provider>

  )

}

export default ReactNavigator;

here is my navigation-service file

import { NavigationActions } from 'react-navigation';

let navigator;

function setTopLevelNavigator(nav_ref) { navigator = nav_ref }

function navigate(route_name, params) {
  navigator.dispatch(
    NavigationActions.navigate({
      route_name,
      params
    })
  )
}

export default {
  navigate,
  setTopLevelNavigator
};

and here is the Provider from which i want to navigate

it's a React Context API

import NavigationService from '@utils/navigation-service';

onReceived(notification) {

  console.warn(notification.payload.additionalData);

  console.warn('triggered');

  NavigationService.navigate('Chat');

}

回答1:


Your route_name should be routeName instead according to the example on react-navigation website (https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html)

They require the keys routeName & params specifically to work



来源:https://stackoverflow.com/questions/54577861/react-navigation-navigate-without-navigation-props-example-not-working

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