如何在React Navigation中使用NavigationService重置导航器?

问题描述 投票:0回答:2

我通过开发我的应用程序在React Navigation中使用NavigationService method。我有一个使用Redux开发的Auth页面。当用户登录Auth页面时,页面将链接到主页面。

但是,我可以通过滑动(在IOS中)或按下后退按钮(在Android中)返回到Auth页面。如何在用户登录或通过Auth页面后重置导航器?

以下是我的登录过程功能:

const loginSuccess = (dispatch, user) => {

   console.log('LOGINSUCCESS --> OK!');
  return dispatch => {
    NavigationService.navigate('Main');
    dispatch({
      type: LOGIN_USER_SUCCESS,
      payload: user,
    });
  };
};
react-native react-navigation
2个回答
0
投票

它不需要从方法返回。它将直接移动到特定的屏幕。

dispatch(
    NavigationService.navigate('Main')
  );

0
投票

我已经解决了这个问题。

首先,我想在下面显示我的NavigationService.js代码:

import { NavigationActions, StackActions } from "react-navigation";

let _navigator;

function setTopLevelNavigator(navigatorRef) {
  _navigator = navigatorRef;
}

function navigate(routeName, params) {
  _navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    }),
  );
}

// add other navigation functions that you need and export them

export default {
  navigate,
  navigateReset,
  setTopLevelNavigator,
};

然后,我添加了一些用于重置导航器的代码。 (这意味着当您通过下一个屏幕但从未通过滑动或Android后退按钮再次返回时。)

function navigateReset(routeName, params) {
  _navigator.dispatch(
    StackActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({
          routeName,
          params,
        }),
      ],
    }),
  );
}

最后,我用新功能编辑了我的Action代码:

const loginSuccess = (dispatch, user) => {
  firebase
    .database()
    .ref('/users/' + user.user.uid)
    .once('value')
    .then((snapshot) => {
      dispatch({
        type: LOGIN_USER_SUCCESS,
        payload: user,
        myUser: {
          uid: user.user.uid,
          email: user.user.email,
          name: snapshot.val().displayName,
          groups: snapshot.val().groups,
        },
      });
    });

  dispatch(() => {
    NavigationService.navigateReset('Main'); // Edited code.
  });

  console.log('LOGINSUCCESS --> OK!');
};

Taa taa!恭喜!现在,您在登录后永远不会返回登录屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.