axios 使用initialRouteName react native时堆栈导航不工作

ax6ht2ek  于 2023-03-12  发布在  iOS
关注(0)|答案(1)|浏览(185)

使用处理登录的本地堆栈导航器login.js import响应本地应用程序。当在堆栈中调用login作为初始路径时(initialRouteName=“Login”),堆栈在登录成功时不会导航到主堆栈,但在从抽屉导航到登录时,它实际上会工作。
所以简而言之,登录确实起作用了,但当它作为初始路由被调用时就不行了...
下面是app.js文件中的代码:

const Stack = createNativeStackNavigator();
HomeStack = () => {
  // const [showSplash, setShowSplash] = useState(true);
  useEffect(() => {
    Translation.setLanguage('en');
    // setTimeout(() => {
    //   setShowSplash(false);
    // }, 2000);
  }, []);
return (
    <Stack.Navigator
      initialRouteName="Login"
      screenOptions={{
        headerShown: false,
      }}
    >
      <>
        <Stack.Screen name="preloader" component={Preloader} />
        <Stack.Screen name="Login" component={Login} />
        <Stack.Screen name="StackHome" component={HomeDrawer} />
        <Stack.Screen name="Signup" component={Signup} />
          </>
    </Stack.Navigator>
  );
};

const App = () => {
  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <NavigationContainer>{HomeStack()}</NavigationContainer>
      </PersistGate>
    </Provider>
  );
};

export default App;

下面是login.js代码:

const Login = ({navigation}) => {
  const settings = useSelector(state => state.setting.settings);
  const isFocused = useIsFocused();
  const dispatch = useDispatch();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [secureP, setSecureP] = useState(true);
  const [emailValid, setEmailValid] = useState(false);
  const [loader, setLoader] = useState(false);
  const [showAlert, setShowAlert] = useState(false);
  const [type, setType] = useState('');
  const [title, setTitle] = useState('');
  const [desc, setDesc] = useState('');
  const [loading, setLoading] = useState(false);
  const navigationforword = useNavigation();

  const validateEmail = userEmail => {
    let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/;
    if (reg.test(userEmail) === false) {
      setEmailValid(false);
      return false;
    } else {
      setEmailValid(true);
      setEmail(userEmail);
    }
  };
  const login = () => {
    setLoading(true);
    if (email != '' && password != '') {
      axios
        .post(Constant.BaseUrl + 'user/do_login', {
          username: email,
          password: password,
        })
        .then(async response => {
          console.log("response",response)
          if (response.data.type == 'success') {
            setLoading(false);
            // if (response.data.userDetails.is_deactivated == "false") {
            dispatch(updateToken(response.data.authToken));
            dispatch(updateUserInfo(response.data.profile.umeta));
            dispatch(updateProfileInfo(response.data.profile.pmeta));
            dispatch(updateBilling(response.data.profile.billing));
            dispatch(updateShipping(response.data.profile.shipping));
            dispatch(
              updateProfileImage(response.data.profile.pmeta.profile_img),
            );
            dispatch(
              updateProfileBannerImage(response.data.profile.pmeta.banner_img),
            );
            dispatch(updateProfileName(response.data.profile.pmeta.full_name));
            dispatch(updateVerified(response.data.profile.pmeta._is_verified));
            navigation.navigate('StackHome');
            // navigation.reset({
            //   index: 2,
            //   routes: [{name: 'DashboardTab'}],
            // });
            // } else {
            //   setUserId(response.data.userDetails.userId);
            //   setUserToken(response.data.authToken.authToken);
            //   RBSheetRestoreAccount.current.open();
            // }
          } else if (response.data.type == 'error') {
            setLoading(false);
            setShowAlert(true);
            setType(response.data.type);
            setTitle(response.data.title);
            setDesc(response.data.message);
          }
        })
        .catch(error => {
          setLoading(false);
          console.log(error);
        });
    } else {
      setShowAlert(true);
      setType('error');
      setTitle(Translation.globalOops);
      setDesc(Translation.globalEnterData);
      setLoading(false);
    }
  };
  const hideAlert = () => {
    setShowAlert(false);
  };
 // return the view etc...
pkmbmrz7

pkmbmrz71#

问题是调用相同组件的抽屉中的冲突:

<Drawer.Screen name="Login" component={Login} />

移除后,登录功能符合预期,并导航至主屏幕。

相关问题