React-navigation Routing Level login interception

Source: Internet
Author: User

Routing stacks

Const Navigator = stacknavigator ({
    Setting: {screen:settingscreenview},
    ...
});

1. Set Routing status Change Intercept

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. Login Jump Intercept

The route to intercept logins is const NEEDLOGINROUTE = [' Setting '];

Const Defaultgetstateforaction = StartScreen.router.getStateForAction;
    StartScreen.router.getStateForAction = (action, state) + = {Let RouteName = Dealmsgdeal (action.routename); Intercept needs to log in after the jump is also not 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);


};
    Required to intercept login page function dealmsgdeal (routeName) {Let theroutename = '; if (RouteName) {for (var i in Needloginroute) {if (needloginroute[i] = = routeName) {T
                Heroutename = RouteName;
            Break
}}} return theroutename; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.