【问题标题】:React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got undefinedReact.createElement:类型无效 - 需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但未定义
【发布时间】:2020-09-04 17:48:23
【问题描述】:

我刚刚开始学习如何使用 React Native 创建应用程序。在了解了 Stack Navigator 之后,我尝试尝试一下,但我收到了这个错误。警告:React.createElement:类型无效 - 需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:%s.%s%s,未定义,您可能忘记导出您的组件来自定义它的文件,或者您可能混淆了默认导入和命名导入。

App.js

import React from 'react';
import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationNativeContainer } from '@react-navigation/native';
import {createStackNavigator}  from '@react-navigation/stack';


const Stack = createStackNavigator();
export default function App() {
    return( 
    <NavigationNativeContainer>
      <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen}/>
       </Stack.Navigator>
      </NavigationNativeContainer>
    );
}
const HomeScreen=(props)=> {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
    </View>
  );
}

包.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.10",
    "@react-navigation/core": "^5.7.0",
    "@react-navigation/native": "^5.4.0",
    "@react-navigation/stack": "^5.3.7",
    "expo": "~37.0.3",
    "expo-font": "~8.1.0",
    "native-base": "^2.13.12",
    "react": "~16.9.0",
    "react-dom": "~16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "^1.8.0",
    "react-native-safe-area-context": "^1.0.0",
    "react-native-screens": "^2.7.0",
    "react-native-web": "~0.11.7"
  },
  "devDependencies": {
    "@babel/core": "^7.8.6",
    "babel-preset-expo": "~8.1.0"
  },
  "private": true
}

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    改变

    import { NavigationNativeContainer } from '@react-navigation/native';
    

    import { NavigationContainer } from '@react-navigation/native';
    
    
      <NavigationContainer>
      <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen}/>
       </Stack.Navigator>
      </NavigationContainer>
    

    react-navigation 在 beta 的时候是 NavigationNativeContainer,现在是 NavigationContainer

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我有一个对我有用的替代解决方案。在 Stack.Navigator 中,我将 headerMode 属性设置为“none”。

      <HabitStack.Navigator
        headerMode="none"
        initialRouteName="HabitManagerScreen"
      >
        <HabitStack.Screen
          name="HabitManagerScreen"
          component={HabitManagerScreen}
        />
        <HabitStack.Screen name="HabitStatsScreen" component={HabitStatsScreen} />
      </HabitStack.Navigator>
      

      【讨论】:

      • 添加 headerMode="none" 的问题是它删除了标题。
      【解决方案3】:

      当你使用native-baseImage组件时会出现这个错误:

      import {Image} from 'native-base';
      
      <Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />
      

      改用react-native Image 组件:

      import {Image} from 'react-native';
      
      <Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-29
        • 2022-01-12
        • 2017-08-30
        • 1970-01-01
        • 2020-03-07
        • 2021-06-30
        • 2020-12-17
        • 2021-05-07
        相关资源
        最近更新 更多