问题
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