带有反应导航的 Expo SDK 45 启动画面

Expo SDK 45 splash screen with react navigation

我刚刚升级到 Expo SDK 45,我收到一条警告:“expo-app-loading 已被弃用,取而代之的是 expo-splash-screen:使用 SplashScreen.preventAutoHideAsync() 和 SplashScren.hideAsync( ) 相反。https://docs.expo.dev/versions/latest/sdk/splash-screen/。所以我做了并遵循了提供的 link。

我现在遇到的问题是,在示例中,他们在根视图的 onLayOut 上调用了 onLayOutRootView。现在我正在使用 react-navigation,所以我的根视图嵌套在我的应用程序中很深。

我是否必须将这个函数向下传递到根视图,或者是否有办法将这个函数传递到我的 providers/navigationcontainer 之一?或者任何其他修复?

//imports


export default App = () => {
  const [appIsReady, setAppIsReady] = useState(false);
  const scheme = "dark";

  useEffect(() => {
    async function prepare() {
      try {
        // Keep the splash screen visible while we fetch resources
        await SplashScreen.preventAutoHideAsync();
        // Pre-load fonts, make any API calls you need to do here
        await Font.loadAsync(customFonts);
      } catch (e) {
        console.warn(e);
      } finally {
        // Tell the application to render
        setAppIsReady(true);
      }
    }

    prepare();
  }, []);

  const onLayoutRootView = useCallback(async () => {
    if (appIsReady) {
      await SplashScreen.hideAsync();
    }
  }, [appIsReady]);

  if (appIsReady) {
    return (
      <StripeProvider publishableKey={PUBLISHABLE_KEY}>
        <ThemeProvider theme={scheme === "dark" ? darkTheme : lightTheme}>
          <StatusBar barStyle={scheme === "dark" ? "light-content" : "dark-content"} />
          <OrderProvider>
            <CartProvider>
              <FavoriteProvider>
                <FirebaseProvider>
                  <UserProvider>
                    <NavigationContainer
                      theme={scheme === "dark" ? darkTheme : lightTheme}
                      ref={navigationRef}
                    >
                      <RootStackScreens on/>
                    </NavigationContainer>
                  </UserProvider>
                </FirebaseProvider>
              </FavoriteProvider>
            </CartProvider>
          </OrderProvider>
        </ThemeProvider>
      </StripeProvider>
    );
  } else {
    return null;
  }
};

谢谢。

您可以尝试用 flex:1.

将所有内容包装在 View

像这样:

...imports...
import { View } from "react-native";
    
export default App = () => {

 ...code...

 const onLayoutRootView = useCallback(async () => {
  if (appIsReady) {
   await SplashScreen.hideAsync();
  }
 }, [appIsReady]);
    
 if (!appIsReady) {
  return null;
 }
    
 return (
  <View style={{ flex: 1 }} onLayout={onLayoutRootView}>
   <StripeProvider publishableKey={PUBLISHABLE_KEY}>
     ...
   </StripeProvider>
  </View>
 );
}