react-native - 任何导航器均未处理有效载荷为 'NAVIGATE' {"name"} 的操作 :"Home"

标签 react-native react-navigation react-navigation-stack

当前行为
出现错误

react 原生的私有(private)路由不适用于页面更改。带有有效负载 {"name":"Home"} 的操作 'NAVIGATE' 未由任何导航器处理。
你有一个名为“主页”的屏幕吗?

如果您试图导航到嵌套导航器中的屏幕,请参阅 https://reactnavigation.org/docs/nesting-navigators#navigating-to-a-screen-in-a-nested-navigator .

这是一个仅限开发的警告,不会在生产中显示。

Error message
预期行为
无法使用身份验证流进行导航。

如何重现
使用的代码来自:https://reactnavigation.org/docs/auth-flow

<NavigationContainer>
        <Stack.Navigator
          screenOptions={{
            headerShown: false,
          }}>
           {isSignedIn === "true"? ( 
            <> 
            <Stack.Screen name="Home" component={Home} />
            <Stack.Screen name="Bookorder" component={Bookorder} />
             </> 
           ) : (
            <>
            <Stack.Screen name="Login" component={Login} />
            </>
          )}
        </Stack.Navigator>
  </NavigationContainer>

环境
系统:
操作系统:macOS 10.15.4
CPU:(4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
内存:46.17 MB/8.00 GB
外壳:3.2.57 -/bin/bash
二进制文件:
节点:13.12.0 -/usr/local/bin/node
yarn :未找到
npm:6.14.4 -/usr/local/bin/npm
守望者:4.9.0 -/usr/local/bin/watchman

最佳答案

我得到一个临时解决方案:

import React, {Component} from 'react';
import {Provider} from 'react-redux';
import configureStore from './redux/store/Store';
import {View} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {
  createStackNavigator,
  CardStyleInterpolators,
} from '@react-navigation/stack';
import HomeRoot from './appRoot/HomeRoot';
import AuthRoot from './appRoot/AuthRoot';
import LocalStorage from './common/LocalStorage';
import {Root} from 'native-base'; //for toast
const store = configureStore();

const RootStack = createStackNavigator();

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isLogin: false,
      isCheckLogin: false,
    };
  }

  setLoginState = async () => {
    let isLogin = await LocalStorage.read('isLogin');
    this.setState({isLogin: isLogin, isCheckLogin: true});
  };

  componentDidMount = async () => {
    await this.setLoginState();
  };

  render() {
    const {isLogin, isCheckLogin} = this.state;
    if (!isCheckLogin) {
      return <View />;
    }

    return (
      <Provider store={store}>
        <Root>
          <NavigationContainer>
            <RootStack.Navigator
              initialRouteName="Home"
              headerMode="none"
              screenOptions={{
                gestureEnabled: false,
                cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
              }}>
              {isLogin ? (
                <>
                  <RootStack.Screen
                    initialParams={{setLogin: this.setLoginState()}}
                    name="Home"
                    component={HomeRoot}
                  />
                </>
              ) : (
                <>
                  <RootStack.Screen
                    initialParams={{setLogin: this.setLoginState()}}
                    name="Login"
                    component={AuthRoot}
                  />
                </>
              )}
            </RootStack.Navigator>
          </NavigationContainer>
        </Root>
      </Provider>
    );
  }
}
export default App;

关于react-native - 任何导航器均未处理有效载荷为 'NAVIGATE' {"name"} 的操作 :"Home",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61575467/

相关文章:

react-native - 如何从抽屉屏幕更新标题栏

react-native - 如何从抽屉导航器菜单导航到特定选项卡 native react

javascript - 如何在 React 中过滤屏幕上显示的数组项?

firebase - 在 React Native 中从 Firebase 检索数据

android - React Native 'yarn android' 失败,出现 'Task :app:compileDebugJavaWithJavac FAILED' 和 'package org.unimodules.core.interfaces does not exist'

reactjs - 当 react 导航中的 'navigationRef' 准备好时,我如何监听?

react-native - 导航到屏幕后 react 抽屉导航重新打开

react-native - React Navigation 在 componentDidMount 上更改自定义标题标题

react-native - React Native React 导航标题按钮事件

node.js - 如何修复应显示数据的授权的 [object Object] 响应?