React Native启动画面导航

b91juud3  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(59)

下面的代码是我的启动屏幕,如果位置设置了,这个屏幕必须显示,否则我需要导航到AuthSplashScreen。

componentDidUpdate () {
        debugger;
        if(this.props.location){
                    return this.props.navigation.dispatch(NavigationActions.reset(
                    {
                        index: 0,
                        actions: [
                          NavigationActions.navigate({ routeName: 'Home'})
                        ]
                    }));
                }else{
                     setTimeout(function(){
                        return this.props.navigation.dispatch(NavigationActions.reset(
                        {
                            index: 0,
                            actions: [
                            NavigationActions.navigate({ routeName: 'AuthSplashScreen'})
                            ]
                        }));
                     }, 1000)
                }
    }

字符串
这里的问题是:1)如果我使用settimeout,输出无法读取属性'navigation'的undefined。2)如果我删除settimeout,它将完美地工作,并根据需要导航AuthSplashScreen(当位置未设置时)/Home(当位置设置时)。但是,它给出了一个** Flink **,就像将屏幕从SplashScreen更改为AuthSplashScreen时的显示。

那么如何解决这个 Flink 效果呢?还是需要改变管理屏幕的逻辑?

ylamdve6

ylamdve61#

只需要保留一个对象的引用。

componentDidUpdate () {
        debugger;
        if(this.props.location){
                    return this.props.navigation.dispatch(NavigationActions.reset(
                    {
                        index: 0,
                        actions: [
                          NavigationActions.navigate({ routeName: 'Home'})
                        ]
                    }));
                }else{
                     var thisObj = this;
                     setTimeout(function(){
                        return thisObj.props.navigation.dispatch(NavigationActions.reset(
                        {
                            index: 0,
                            actions: [
                            NavigationActions.navigate({ routeName: 'AuthSplashScreen'})
                            ]
                        }));
                     }, 1000)
                }
    }

字符串

相关问题