javascript - 更新 expo : Element type is invalid: expected a string (for built-in components) or a class/function 后

标签 javascript react-native expo

在我将我的 Expo SDK 从 39 更新到 42 后,我在尝试启动应用程序时遇到以下错误:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of App.

This error is located at: in App (created by ExpoRoot) in ExpoRoot (at renderApplication.js:45) in RCTView (at View.js:34) in View (at AppContainer.js:106) in RCTView (at View.js:34) in View (at AppContainer.js:132) in AppContainer (at renderApplication.js:39)

从错误报告中我得出结论,错误位于我的 App.js 文件中,尽管我除了升级我的 expo 版本之外没有做任何更改。

应用程序.js:

//Automatic imports
import React, {useState} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux';

//My imports
import * as Font from 'expo-font';
import {AppLoading} from 'expo';
import ReduxThunk from 'redux-thunk';
import { createStore, combineReducers, applyMiddleware } from 'redux';


//import other screens
import NavigationContainer from './navigation/NavigationContainer';

//import Reducers
import authReducer from './store/reducers/auth';
import pigeonReducer from './store/reducers/pigeon';
import badgesReducer from './store/reducers/badges';
import languagesReducer from './store/reducers/language';

//imports related to firebase
import * as firebase from "firebase";
import ApiKeys from "./constants/ApiKeys";

//Loading Fonts, returns promise
const fetchFonts = () => {
  return Font.loadAsync({
    'Magnus' : require('./assets/fonts/MagnusText.ttf'),
    'AmaticBold' : require('./assets/fonts/Amatic-Bold.ttf'),
    'AmaticRegular' : require('./assets/fonts/AmaticSC-Regular.ttf'),
    'SEASRN' : require('./assets/fonts/SEASRN.ttf'),
    'Otto' : require('./assets/fonts/Otto.ttf'),
    'Gwibble' : require('./assets/fonts/GWIBBLE.ttf'),
    'GTA' : require('./assets/fonts/pricedown.otf')
  });
};

const rootReducer = combineReducers({
  auth: authReducer,
  myPigeons: pigeonReducer,
  myBadges: badgesReducer,
  myLanguage: languagesReducer
});

const store = createStore(rootReducer, applyMiddleware(ReduxThunk));


export default function App() {
  //firebase project initialisation
  if (!firebase.apps.length) {
    firebase.initializeApp(ApiKeys);
    //console.log("DB INITIALISED"); FIRING IF DATABASE INITIALISED
  };

  const [dataLoaded, setDataLoaded] = useState(false); //are fonts loaded?

  if(!dataLoaded){ //will go into if clause because fonts are not loaded
    return(
      <AppLoading 
        startAsync={fetchFonts} 
        onFinish={() => setDataLoaded(true)}
        onError={(err) => console.log(err)}
      />
    )
  }
  return (
    <Provider store={store}>
      <NavigationContainer/>
    </Provider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

我知道有几个人遇到了同样的错误,但是他们的问题有很多答案对我不起作用,也许有人知道如何解决我的问题。 如果您需要我项目中的其他代码示例,请告诉我,我会更新此问题。

最佳答案

99% 的情况下,此错误表示您正在导入 Xyz来自某个地方,但它实际上并不作为导出存在。

因此当你尝试做 <Xyz ... />在您的 JSX 中,您会收到错误,我将在此处重现:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

(强调我的)

undefined是这里的关键,例如你有

import {AppLoading} from 'expo';

然而,根据当前文档的行是

import AppLoading from 'expo-app-loading';

要调试它,通过简单地记录它们来检查导入的组件:

console.log(AppLoading);

您会很快找到 undefined 的导入,这是导致错误的原因。

关于javascript - 更新 expo : Element type is invalid: expected a string (for built-in components) or a class/function 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68409104/

相关文章:

npm 运行脚本流不起作用

javascript - React Native 中心动画图标

react-native - 获取 someUrl.html 在 React Native/Expo 中不起作用?

javascript - 每 10 秒更改一次图像(作为背景)

javascript - 如何将动画设置为图像后面的 div?

javascript - 如何在 React Native Router Flux 中嵌套场景并使用方向 ='vertical' 导航?

react-native - 是否可以通过托管工作流程将 Apple 和/或 Firebase 推送通知集成到 React Native/Expo 应用程序?

javascript - 如何用服务器、客户端和共享代码来组织 node、js/webpack 项目?

javascript - CasperJS 超时时跳过步骤

ios - android上axios发布请求网络错误