Search code examples
react-nativereact-navigationreact-native-router-flux

Redirection not working on componentWillMount() react-native-router-flux


componentWillMount() {
    var user = UserService.findAll();
    if (user.length > 0) Actions.home();
  }

where 'home' is Scene key in my router.js.

On the other hand

    onButtonPress() {
        var user = UserService.findAll();
        if (user.length > 0) Actions.home();
      }

worked like a charm !

My router.js

const RouterComponent = () => {
  return (
    <Router showNavigationBar={false}>
      <Stack key="root">
        <Scene key="auth" hideNavBar={true}>
          <Scene key="login" component={Login} />
        </Scene>

        <Scene key="home" component={Home} hideNavBar={true}/>
        <Scene key="pdf" component={Pdf} hideNavBar={true} />
      </Stack>
    </Router>
  );
};

export default RouterComponent;

Solution

  • on props did the trick for me

    I need conditional navigation, so I used success and failure attributes with names of scenes to navigate

    I am posting my code below, hope it helps someone

    <Scene
        key="login"
        component={Login}
        on={() => UserService.findAll().length > 0}
        success={()=>
           Actions.replace("home")
        }  
        failure="login"
    />