React Native - NavigationActions.navigate() not navigating from within redux
Asked Answered
W

1

6

I have two navigators one is stackNavigator and another is drawerNavigator. what I want to do is dispatch an action and login is successfull and redirect the user to drawer navigator. I have used react-navigation. What I have done is I am dispatching the action login success in saga. Using NavigationActions.navigate({ routeName: 'drawerStack' }) to dispatch the action. The action dispatches successfully but it doesn't navigate to drawerNavigator as shown in the picture below. What am I doing wrong?

enter image description here

saga.js

function* watchLoginRequest() {
  while (true) {
    const { state } = yield take(LOGIN_REQUEST);

    try {
      const payload = {
        state
      };
      const response = yield call(loginCall, payload);
      yield put(loginSuccess(response));
      yield setUser(response.user);
      yield put(NavigationActions.navigate({ routeName: 'drawerStack' }));
    } catch (err) {
      yield put(loginFailure(err.status));
    }
  }
}

drawerNavigation.js

// drawer stack
const DrawerStack = DrawerNavigator({
  testComponent: { screen: TestComponent },
});

const DrawerNav = StackNavigator({
  drawerStack: { screen: DrawerStack }
}, {
  headerMode: 'float',
  navigationOptions: ({ navigation }) => ({
    headerStyle: { backgroundColor: 'green' },
    title: 'Logged In to your app!',
    headerLeft: <Text onPress={() => navigation.navigate('DrawerOpen')}>Menu</Text>
  })
});

export default DrawerNav;

loginNavigation.js

// login stack
const LoginStack = StackNavigator({

  startScreen: { screen: StartScreen },
  loginScreen: { screen: LoginScreen },
  personalInformation: { screen: PersonalInformation },
  vehicleInformation: { screen: VehicleInformation },
  availability: { screen: Availability },
  selectRegisteration: { screen: SelectRegisteration },
  serviceAddress: { screen: ServiceAddress },

}, {
  headerMode: 'none',
  transitionConfig: TransitionConfiguration

});


export default LoginStack;

ReduxNavigation.js

class ReduxNavigation extends React.Component {
  constructor(props) {
    super(props);
    const { dispatch, nav } = props;
    const navigation = ReactNavigation.addNavigationHelpers({
      dispatch,
      state: nav
    });
    this.state = {
      loggedInStatus: false,
      checkedSignIn: false
    };
  }

  componentWillMount() {
    isSignedIn()
      .then(res => {
        if (res !== null) {
          this.setState({
            loggedInStatus: true,
            checkedSignIn: true
          });
        } else {
          console.log(res);
        }
      })
      .catch(err => console.log(err));
  }

  render() {
    return <LoginNavigation navigation={this.navigation} />;
  }
}

const mapStateToProps = state => ({ nav: state.nav });
export default connect(mapStateToProps)(ReduxNavigation);
Wengert answered 21/3, 2018 at 12:9 Comment(7)
What are you expecting? To navigate to TestComponent or to show the drawer? To show the drawer you want .navigate('DrawerOpen')Tannic
Actually I am trying to navigate to TestComponent.Wengert
Its neither opening the drawer if I .navigate('DrawerOpen')Wengert
Where are you adding your drawer navigator to the navigation stack? You either aren't providing all the code, or you don't have your navigation stacks set up properly. DrawerNavigator needs to be added to the stack if you want to be able to navigate to any of it's pathsTannic
yup, I guess I need to configure and add navigation helpers properly so that both of my navigators are in sync with redux. Anyway thanks for your concern.Wengert
Personally, I found the navigation to be a bit confusing. I'd suggest starting with a basic setup without redux and getting an understanding of that. Then you can add redux. AFAIK, you can't modify the navigation stack on the fly, which is what it sounds like you might be trying to do. See github.com/react-navigation/react-navigation/issues/71Tannic
yes, I agree with you.Wengert
T
0

To navigate to TestComponent you want your routeName to be testComponent, not the drawer stack. You navigate to specific screens, not navigation components.

Tannic answered 21/3, 2018 at 14:20 Comment(1)
I have also tried to navigate to testComponent and some routeNames like selectRegisteration etc but it does the same, it does't get navigated to either of the screensWengert

© 2022 - 2024 — McMap. All rights reserved.