首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在条件语句中不工作的React导航

在条件语句中不工作的React导航
EN

Stack Overflow用户
提问于 2022-06-09 08:49:25
回答 1查看 229关注 0票数 0

在我的kept本机应用程序中,当在条件语句中使用它时,它会继续返回错误。

操作‘导航’的有效载荷{“名称”:“屏幕名称”}没有被任何导航器处理。你有一个名叫“屏幕名”的屏幕吗?

只有当我最小化屏幕并返回时,它才能起作用,

下面是处理登录过程的代码

代码语言:javascript
复制
import { useNavigation } from "@react-navigation/native"

const navigation = useNavigation()
const onLogin = async (data: FormData) => {
authenticationStore.login(data.email, data.password).then(() => {
  if (authenticationStore.isAuthenticated) {
    navigation.navigate('app', {
      screen: 'home',
    })
  }else if(authenticationStore.response == 'timeout'){
    alert('Request timed out')
  }else{
    alert('Invalid email or password')
  }
}
).catch((error) => {
  console.log(error)
}
)}

这是我的领航员

代码语言:javascript
复制
import React, { useState, useEffect, useRef } from "react"
import { useColorScheme } from "react-native"
import { NavigationContainer, DefaultTheme, DarkTheme, NavigatorScreenParams } from "@react-navigation/native"
import { createNativeStackNavigator } from "@react-navigation/native-stack"
import { navigationRef, useBackButtonHandler } from "./navigation-utilities"
import { SplashNavigator, SplashNavigatorParamList } from "./splash/splash-navigator"
import { AuthNavigator, AuthNavigatorParamList } from "./auth/auth-navigator"
import { BottomNavigator, BottomNavigatorParamList } from "./bottom/bottom-navigator"
import { useStores } from "../models"
import { observer } from "mobx-react-lite"

export type NavigatorParamList = {
  splash: NavigatorScreenParams<SplashNavigatorParamList>
  auth: NavigatorScreenParams<AuthNavigatorParamList>
  app: NavigatorScreenParams<BottomNavigatorParamList>
 }

// Documentation: https://reactnavigation.org/docs/stack-navigator/
const Stack = createNativeStackNavigator<NavigatorParamList>()

const AppStack = () => {
  return (
    <Stack.Navigator
      screenOptions={{
        headerShown: false,
      }}
      initialRouteName="app"
    >
      <Stack.Screen name="app" component={BottomNavigator} />
      {/**  Your screens go here */}
    </Stack.Navigator>
  )
}

const AuthStack = () => {
  return(
    <Stack.Navigator
    screenOptions={{
      headerShown: false,
    }}
    initialRouteName="splash"
  >
    <Stack.Screen name="splash" component={SplashNavigator} />
    <Stack.Screen name="auth" component={AuthNavigator} />
  </Stack.Navigator>
  )
}

interface NavigationProps extends Partial<React.ComponentProps<typeof NavigationContainer>> { }

export const AppNavigator = (props: NavigationProps) => {
  const {authenticationStore} = useStores()
  const colorScheme = useColorScheme()
  useBackButtonHandler(canExit)
   return (
  
    <NavigationContainer
      ref={navigationRef}
      theme={colorScheme === "dark" ? DarkTheme : DefaultTheme}
      {...props}
    >
      {authenticationStore.isAuthenticated ? <AppStack /> : <AuthStack />}
    </NavigationContainer>
  )
}

AppNavigator.displayName = "AppNavigator"

const exitRoutes = ["welcome"]
export const canExit = (routeName: string) => exitRoutes.includes(routeName)

上述代码中的每个条件都可以工作,除了导航不能工作,除非我最小化屏幕并重新打开。我正在积极使用以下软件包

代码语言:javascript
复制
   "@react-navigation/material-bottom-tabs": "^6.2.1",
    "@react-navigation/native": "~6.0.1",
    "@react-navigation/native-stack": "^6.0.2",
    "@react-navigation/stack": "~6.0.1",

注意,我使用的是内置点火板。并尝试使用不同的导航方法,但仍然得到了相同的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-17 16:12:16

我必须了解到,我并没有像以前那样在我的导航堆栈中添加mob观察者,现在导航就可以正常工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72557348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档