元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。反应原生

Posted

技术标签:

【中文标题】元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。反应原生【英文标题】:Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. React-Native 【发布时间】:2021-03-24 00:12:44 【问题描述】:

所以我一直在研究为什么这不起作用并且找不到任何东西。 有谁知道这个错误与什么有关?

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
    
    Check the render method of `App`.
    
    This error is located at:
        in App (created by ExpoRoot)
        in ExpoRoot
        in RCTView (created by View)
        in View (created by AppContainer)
        in RCTView (created by View)
        in View (created by AppContainer)
        in AppContainer
    - node_modules\expo\build\logs\LogSerialization.js:160:14 in _captureConsoleStackTrace
    - node_modules\expo\build\logs\LogSerialization.js:41:26 in serializeLogDataAsync
    - ... 9 more stack frames from framework internals

这是我的 App.js:

import * as Icon from '@expo/vector-icons'
import * as AppLoading  from 'expo-app-loading'
import Asset from 'expo-asset'
import * as Font from 'expo-font'
import React from 'react'
import  StatusBar, StyleSheet, View  from 'react-native'
import AppNavigator from './navigation/AppNavigator'

export default class App extends React.Component 
  state = 
    isLoadingComplete: false,
  

  render() 
    if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen) 
      return (
        <AppLoading
          startAsync=this._loadResourcesAsync
          onError=this._handleLoadingError
          onFinish=this._handleFinishLoading
        />
      )
     else 
      return (
        <View style=styles.container>
          <StatusBar hidden />
          <AppNavigator />
        </View>
      )
    
  

  _loadResourcesAsync = async () => 
    return Promise.all([
      Asset.loadAsync([
        require('./assets/images/splash.png'),
        require('./assets/images/icon.png'),
      ]),
      Font.loadAsync(
        //This is the font that we are using for our tab bar
        ...Icon.MaterialIcons.font,
        ...Icon.MaterialCommunityIcons.font,
        ...Icon.FontAwesome.font,
        ...Icon.Feather.font,
      ),
    ])
  

  _handleLoadingError = error => 
    // In this case, you might want to report the error to your error
    // reporting service, for example Sentry
    console.warn(error)
  

  _handleFinishLoading = () => 
    this.setState( isLoadingComplete: true )
  


const styles = StyleSheet.create(
  container: 
    flex: 1,
    backgroundColor: '#fff',
  ,
)

我已经安装了所有必要的模块,重置了缓存,检查了导入语句,但似乎没有任何效果。

【问题讨论】:

【参考方案1】:

请关注展会文档:https://docs.expo.io/versions/latest/sdk/app-loading。

替换

import * as AppLoading from 'expo-app-loading'

通过

import AppLoading from 'expo-app-loading'

解决您的问题。

【讨论】:

以上是关于元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。反应原生的主要内容,如果未能解决你的问题,请参考以下文章

Next Js 错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义

不变违规:元素类型无效:期望字符串(用于内置组件)或类/函数,但得到:未定义

如何修复元素类型无效:期望字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象

ReactApp:错误:元素类型无效:期望字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象 [重复]

错误:元素类型无效:需要一个字符串(对于内置组件)reactjs

Redux 存储:元素类型无效:应为字符串