React Native - NavigationActions.navigate()不从redux内导航

时间:2018-03-21 12:09:24

标签: reactjs react-native react-redux react-navigation redux-saga

我有两个导航器,一个是stackNavigator,另一个是drawerNavigator。 我想要做的是派遣一个动作,登录成功,并将用户重定向到抽屉导航器。我用过react-navigation。 我所做的是我在saga中调度动作登录成功。 使用NavigationActions.navigate({ routeName: 'drawerStack' })分派操作。 操作成功发送,但它没有导航到drawerNavigator,如下图所示。我做错了什么?

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);

1 个答案:

答案 0 :(得分:0)

要导航到TestComponent,您希望routeNametestComponent,而不是抽屉堆栈。您导航到特定屏幕,而不是导航组件。