简体   繁体   English

如何在React Naitve中卸载组件

[英]How to unmount the component in React Naitve

I have created a splash screen with progress bar. 我创建了带有进度条的初始屏幕。 The whole function of the splash screen is to check whether user is available and if yes, it will redirect to Dashboard else Signup form. 初始屏幕的整个功能是检查用户是否可用,如果可以,它将重定向到“仪表板其他注册”表单。 I am using redux action to calculate progress bar value and login check. 我正在使用redux操作来计算进度条值和登录检查。

Here is my Progress Bar Action Code : 这是我的进度栏操作代码:

export const progressBarLevel = () => {
return (dispatch) => {
    this.progress = 0;
    dispatch({ type: PROGRESS, payload: 0 });
    setTimeout(() => {
        dispatch({ type: INDETERMINATE, payload: false });
       const interval = setInterval(() => {
            this.progress += Math.random()/5;
            if(this.progress > 1){
                this.progress = 1;
                clearInterval(interval);
            }
            return dispatch({ type: PROGRESS, payload: this.progress });
        },250)
    },500)
  }
}

And my login Check Action : 而我的登录Check Action:

export const loginCheck = () => {
return async (dispatch) => {
    let token = await AsyncStorage.getItem(LOGIN_TOKEN);
    let userUid = await AsyncStorage.getItem(USER_UID);
    if(token && userUid) {
        dispatch({ type: VALID_LOGIN_AVAILABLE, payload: token });
        dispatch({ type: VALID_UID_AVAILABLE, payload: userUid });
    }
    else {
        dispatch({ type: NO_VALID_LOGIN_AVAILABLE, payload: token });
    }
  }
};

So, by using these i am populating the needs. 因此,通过使用这些,我可以满足需求。 And i am using componentDidMount and willUpdate to perfom actions. 我正在使用componentDidMount和willUpdate进行perfom操作。

My componentDidMount(): 我的componentDidMount():

componentDidMount() {
   this.props.loginCheck();
   this.props.progressBarLevel();
}

My componentDidUpdate(): 我的componentDidUpdate():

componentDidUpdate() {
this.mounted = true;
    if(this.props.progressed === 1) {
        if(this.props.token === null) {
            this.props.navigation.navigate('WelcomeAuth')
        }
        else {
            this.props.navigation.navigate('Dashboard')
        }
    }

}. }。

So, over all everything is working correctly, but when there is no user availble. 因此,总的来说,一切正常,但是当没有用户可用时。 The pages get redirect to "WelcomeAuth" ( actually its correct) But, when i enter something , it again redirects to "Beginning of the page", As a welcome page. 页面被重定向到“ WelcomeAuth”(实际上是正确的)但是,当我输入内容时,它再次重定向到“页面开始”,作为欢迎页面。 So then, i should be clicking on signup button and enter next word, the again it goes back to Welcome page. 因此,我应该单击注册按钮并输入下一个单词,然后再次返回“欢迎”页面。

How to stop the splashscreen component ? 如何停止启动画面组件? As its work is to redirect thats it, after redirect, the splash screen should be unmounted so that, it wont work on background ? 由于其工作是重定向多数民众赞成在重定向之后,应将初始屏幕卸载,以使其在后台不起作用?

How to achieve this ? 如何实现呢?

First create a action to reset the navigation stact 首先创建一个操作以重置导航状态

const homeAction = StackActions.reset({
    index: 0,
    actions: [NavigationActions.navigate({ routeName: 'HomeScreen' })],
});

then navigate using dispatch 然后使用调度导航

this.props.navigation.dispatch(homeAction);

here all other components will be removed and only HomeScreen will be in the navigation stack 在这里,所有其他组件将被删除,并且只有HomeScreen将在导航堆栈中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM