运行 React Native App 中的错误:元素类型无效
Error In Running a React Native App : Element type is invalid
图片链接:
[1]: https://i.stack.imgur.com/8PwUH.jpg
[2]: https://i.stack.imgur.com/MiaA6.jpg
[3]: https://i.stack.imgur.com/x0Pfx.jpg
错误:元素类型无效:应为字符串(对于内置组件)或 class/function(对于复合组件)但得到:未定义。您可能忘记从定义它的文件中导出您的组件,或者您可能混淆了默认导入和命名导入。
检查 App
.
的渲染方法
import React, { useState } from "react";
import { NavigationContainer } from "@react-navigation/native";
import { AppLoading } from "expo";
import AppNavigator from "./app/navigation/AppNavigator";
import AuthNavigator from "./app/navigation/AuthNavigator";
import navigationTheme from "./app/navigation/navigationTheme";
import navigationRef from "./app/navigation/rootNavigation";
import OfflineNotice from "./app/components/OfflineNotice";
import AuthContext from "./app/auth/context";
import authStorage from "./app/auth/storage";
export default function App() {
const [user, setUser] = useState();
const [isReady, setIsReady] = useState(false);
const restoreUser = async () => {
const user = await authStorage.getUser();
if (user) setUser(user);
};
if (!isReady)
return (
<AppLoading
startAsync={restoreUser}
onFinish={() => setIsReady(true)}
/>
);
return (
<AuthContext.Provider value={{ user, setUser }}>
<OfflineNotice />
<NavigationContainer ref={navigationRef} theme={navigationTheme}>
{user ? <AppNavigator /> : <AuthNavigator />}
</NavigationContainer>
</AuthContext.Provider>
);
}
默认导入不需要括号 {}。
此外,您必须从 expo-app-loading 导入 AppLoading,请参阅以下文档:https://docs.expo.io/versions/latest/sdk/app-loading/
解决这个变化
import { AppLoading } from "expo";
至
import AppLoading from 'expo-app-loading'
希望对您有所帮助
图片链接: [1]: https://i.stack.imgur.com/8PwUH.jpg [2]: https://i.stack.imgur.com/MiaA6.jpg [3]: https://i.stack.imgur.com/x0Pfx.jpg
错误:元素类型无效:应为字符串(对于内置组件)或 class/function(对于复合组件)但得到:未定义。您可能忘记从定义它的文件中导出您的组件,或者您可能混淆了默认导入和命名导入。
检查 App
.
import React, { useState } from "react";
import { NavigationContainer } from "@react-navigation/native";
import { AppLoading } from "expo";
import AppNavigator from "./app/navigation/AppNavigator";
import AuthNavigator from "./app/navigation/AuthNavigator";
import navigationTheme from "./app/navigation/navigationTheme";
import navigationRef from "./app/navigation/rootNavigation";
import OfflineNotice from "./app/components/OfflineNotice";
import AuthContext from "./app/auth/context";
import authStorage from "./app/auth/storage";
export default function App() {
const [user, setUser] = useState();
const [isReady, setIsReady] = useState(false);
const restoreUser = async () => {
const user = await authStorage.getUser();
if (user) setUser(user);
};
if (!isReady)
return (
<AppLoading
startAsync={restoreUser}
onFinish={() => setIsReady(true)}
/>
);
return (
<AuthContext.Provider value={{ user, setUser }}>
<OfflineNotice />
<NavigationContainer ref={navigationRef} theme={navigationTheme}>
{user ? <AppNavigator /> : <AuthNavigator />}
</NavigationContainer>
</AuthContext.Provider>
);
}
默认导入不需要括号 {}。 此外,您必须从 expo-app-loading 导入 AppLoading,请参阅以下文档:https://docs.expo.io/versions/latest/sdk/app-loading/
解决这个变化
import { AppLoading } from "expo";
至
import AppLoading from 'expo-app-loading'
希望对您有所帮助