Hey guys, the Expo Apploading component is deprecated. But expo publish the new way to create your splash screen.
You can check it out the Expo Splashscreen official page.
Here’s the code example,
First install the package:
npx expo install expo-splash-screen
Import the package:
import * as SplashScreen from "expo-splash-screen";
After that you can use the preventAutoHideAsync() function for keeping the splash screen visible while we fetch resources:
SplashScreen.preventAutoHideAsync();
Next step is hiding the splash screen:
const onLayoutRootView = useCallback(async () => {
if (isReady) {
// This tells the splash screen to hide immediately! If we call this after
// `setAppIsReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
await SplashScreen.hideAsync();
}
}, [isReady]);
And finally wrap your components inside a view that have onLayoutRootView attribute:
<View style={{ flex: 1 }} onLayout={onLayoutRootView}>
<AuthContext.Provider value={{ user, setUser }}>
<OfflineNotice />
<GestureHandlerRootView style={{ flex: 1 }}>
<NavigationContainer theme={navigationTheme}>
{user ? <AppNavigator /> : <AuthNavigator />}
</NavigationContainer>
</GestureHandlerRootView>
</AuthContext.Provider>
</View>
Please reply for advice or edit.
My full code is below:
Full Code of App.js
import { NavigationContainer } from "@react-navigation/native";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { View } from "react-native";
import { useEffect, useState, useCallback } from "react";
import { jwtDecode } from "jwt-decode";
import * as SplashScreen from "expo-splash-screen";
import navigationTheme from "./app/navigation/navigationTheme";
import AppNavigator from "./app/navigation/AppNavigator";
import OfflineNotice from "./app/components/OfflineNotice";
import AuthNavigator from "./app/navigation/AuthNavigator";
import AuthContext from "./app/auth/context";
import authStorage from "./app/auth/storage";
SplashScreen.preventAutoHideAsync();
export default function App() {
const [user, setUser] = useState();
const [isReady, setIsReady] = useState(false);
const restoreToken = async () => {
const token = await authStorage.getToken();
if (!token) return;
setUser(jwtDecode(token));
};
const prepare = async () => {
await restoreToken();
setIsReady(true);
};
useEffect(() => {
prepare();
}, []);
const onLayoutRootView = useCallback(async () => {
if (isReady) {
// This tells the splash screen to hide immediately! If we call this after
// `setAppIsReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
await SplashScreen.hideAsync();
}
}, [isReady]);
if (!isReady) {
return null;
}
return (
/*
FIXME: FIXED upgrade the expo to @latest("^51.0.16")
The following packages should be updated for best compatibility with the installed expo version:
expo@51.0.16 - expected version: ~51.0.17
Your project may not work correctly until you install the expected versions of the packages.
*/
//GestureHandlerRootView removed from ListItem.js.
//Swipeable component was wrapped inside.
<View style={{ flex: 1 }} onLayout={onLayoutRootView}>
<AuthContext.Provider value={{ user, setUser }}>
<OfflineNotice />
<GestureHandlerRootView style={{ flex: 1 }}>
<NavigationContainer theme={navigationTheme}>
{user ? <AppNavigator /> : <AuthNavigator />}
</NavigationContainer>
</GestureHandlerRootView>
</AuthContext.Provider>
</View>
);
}