我尝试在react-native v3.11.0的Navigating without the navigation prop
上使用该示例。
但是我得到了这个错误:
TypeError: NavigationService._navigator.dispatch is not a function
at Function.navigate (C:\...\NavigationService.js)
有人可以帮我吗?
更新:
我的代码:
import React from 'react';
import {NavigationActions, StackActions} from 'react-navigation';
export default class NavigationService extends React.Component {
static _navigator;
static setTopLevelNavigator(navigatorRef) {
NavigationService._navigator = navigatorRef;
}
static navigate(routeName, params = null) {
let options = params == null ? {routeName} : {routeName, params};
NavigationService._navigator.dispatch(NavigationActions.navigate(options));
}
}
以及在App.js上:
render() {
return (
<Navigation
ref={(navigatorRef) => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
);
}
答案 0 :(得分:0)
您的代码与文档不同。尝试使用文档中的代码
// NavigationService.js
import { NavigationActions } from 'react-navigation';
let _navigator;
function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;
}
function navigate(routeName, params) {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
})
);
}
// add other navigation functions that you need and export them
export default {
navigate,
setTopLevelNavigator,
};