react-navigation 路由級登入攔截

來源:互聯網
上載者:User

// 路由棧

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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.