// 路由棧
const Navigator = StackNavigator({ Setting: {screen: SettingSCreenView}, ....});
1、設定路由狀態改變攔截
function getCurrentRouteName(navigationState) { if (!navigationState) { return null; } const route = navigationState.routes[navigationState.index]; // dive into nested navigators if (route.routes) { return getCurrentRouteName(route); } return route.routeName;}class StartScreen extends Component { render() { const prefix = Platform.OS === 'android' ? 'xx://aa/' : 'xx://'; return ( <Navigator uriPrefix={prefix} onNavigationStateChange={ (prevState, currentState) => { const currentScene = getCurrentRouteName(currentState); if(currentScene == 'Setting'){ // to do something } } />); }
2、登入跳轉攔截
// 攔截登入的路由const needLoginRoute = ['Setting'];const defaultGetStateForAction = StartScreen.router.getStateForAction;StartScreen.router.getStateForAction = (action, state) => { let routeName = dealMsgDeal(action.routeName); // 攔截需要登入後跳轉的也沒 if(action.routeName === routeName && islogin == false){ this.routes = [ ...state.routes, {key: 'id-'+Date.now(), routeName: 'Login', params: { name: routeName, params: action.params}}, ]; return { ...state, routes, index: this.routes.length - 1, }; } return defaultGetStateForAction(action, state);};// 需要攔截登入的頁面function dealMsgDeal(routeName){ let theRouteName = ''; if(routeName){ for (var i in needLoginRoute) { if (needLoginRoute[i] == routeName) { theRouteName = routeName; break; } } } return theRouteName;}