React Native 错误:路由组件必须是 React 组件

发布于 2025-01-20 05:27:32 字数 2756 浏览 0 评论 0原文

我正在尝试为React导航创建一个基本设置,但是由于某种原因,当我去查看项目时,我会在终端中获得一个空白屏幕和错误,说:

Error: The component for route 'screens' must be a React component. For example:

import MyScreen from './MyScreen';
...
screens: MyScreen,
}

You can also use a navigator:

import MyNavigator from './MyNavigator';
...
screens: MyNavigator,
}

我查看了其他堆栈溢出解决方案,但是在我的情况下,它们似乎都不适用,所以在这里我做错了什么?

我的 app.js (也在这里导入字体,但这些效果,这似乎是路由的问题)

import React, {useState} from 'react';
import * as Font from 'expo-font';
import AppLoading from 'expo-app-loading';
import Navigator from './routes/homeStack';

const getFonts = () => Font.loadAsync({
    'raleway-regular': require('./assets/fonts/Raleway-Regular.ttf'),
    'raleway-bold': require('./assets/fonts/Raleway-Bold.ttf')
  });
export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);
  
  if (fontsLoaded) {
    return (
      <Navigator />
    );
  } else {
    return (
      <AppLoading
        startAsync= {getFonts}
        onFinish= {()=> setFontsLoaded(true)}
        onError= {()=> console.log('error')} 
      />
    );
  }
}

homestack.js

import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
import Home from '../screens/home';
import Scanner from '../screens/scanner';

const screens = {
    Home: {
        screen: Home
    },
    Scanner: {
        screen: Scanner
    },
};

const HomeStack = createStackNavigator({screens});
export default createAppContainer(HomeStack);

>

import React from 'react';
import { StyleSheet, View, Text, } from 'react-native';
import { globalStyles } from '../styles/global';

export default function Home() {
  return (
    <View style={globalStyles.container}>
      <Text style={globalStyles.titleText}>Home Screen</Text>
    </View>
  );
}

scanner.js

import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { globalStyles } from '../styles/global';

export default function Scanner() {
  return (
    <View style={globalStyles.container}>
      <Text>About Screen</Text>
    </View>
  );
}

我的文件目录

“在此处输入图像说明”

任何帮助都将不胜感激!

I'm trying to create a basic setup for react navigation, but for some reason, when I go to view the project, I get a blank screen and an error in terminal that says:

Error: The component for route 'screens' must be a React component. For example:

import MyScreen from './MyScreen';
...
screens: MyScreen,
}

You can also use a navigator:

import MyNavigator from './MyNavigator';
...
screens: MyNavigator,
}

I've looked at other Stack Overflow solutions, but none of them seem to apply in my case, so, is there something else I'm doing wrong here?

My App.js (Also importing fonts here, but these worked, it seems to be an issue with the routing)

import React, {useState} from 'react';
import * as Font from 'expo-font';
import AppLoading from 'expo-app-loading';
import Navigator from './routes/homeStack';

const getFonts = () => Font.loadAsync({
    'raleway-regular': require('./assets/fonts/Raleway-Regular.ttf'),
    'raleway-bold': require('./assets/fonts/Raleway-Bold.ttf')
  });
export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);
  
  if (fontsLoaded) {
    return (
      <Navigator />
    );
  } else {
    return (
      <AppLoading
        startAsync= {getFonts}
        onFinish= {()=> setFontsLoaded(true)}
        onError= {()=> console.log('error')} 
      />
    );
  }
}

homeStack.js

import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
import Home from '../screens/home';
import Scanner from '../screens/scanner';

const screens = {
    Home: {
        screen: Home
    },
    Scanner: {
        screen: Scanner
    },
};

const HomeStack = createStackNavigator({screens});
export default createAppContainer(HomeStack);

home.js

import React from 'react';
import { StyleSheet, View, Text, } from 'react-native';
import { globalStyles } from '../styles/global';

export default function Home() {
  return (
    <View style={globalStyles.container}>
      <Text style={globalStyles.titleText}>Home Screen</Text>
    </View>
  );
}

scanner.js

import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { globalStyles } from '../styles/global';

export default function Scanner() {
  return (
    <View style={globalStyles.container}>
      <Text>About Screen</Text>
    </View>
  );
}

My file directory

enter image description here

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2025-01-27 05:27:32

您正在关注的视频确实很旧,可能不再是最新的。请遵循安装指南,然后按照本指南。这应该可以让您在几分钟内启动并运行。

The video you are following along with is really old and probably not up to date anymore. Please follow the installation guides and then follow along this guide. That should get you up and running in minutes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文