React Native - Navigate after an async action

Viewed 3304

I'm developing a mobile app with React Native and Redux and I'm facing a software design problem. I want to call a REST API (async operation) for login and navigate to main view if that operation was successful. I'm using redux and thunk so I already have the async actions implemented so my main doubt is: Where should I put the logic to navigate to main view?

Can I access the navigator object directly from an action and perform the navigation there? Should I do that in the Login Component? (As I'm doing it already - check the code below).

componentWillReceiveProps(nextProps){
    if(nextProps.errorLoginMsg){
        Alert.alert("Login Failed", nextProps.errorLoginMsg);
    }
    else if(!nextProps.user.isNull()){
      this.props.navigator.replace({name: 'main'});
    }
  }

I'm not confident of having that logic in the component. Does not seem a good practice. Are there any other way to do this?

Thanks

2 Answers
Related