【问题标题】:Expo SDK 45 splash screen with react navigation带有反应导航的 Expo SDK 45 闪屏
【发布时间】:2022-07-29 01:50:45
【问题描述】:

我刚刚升级到 Expo SDK 45,我收到一条警告:“不推荐使用 expo-app-loading 来支持 expo-splash-screen:请改用 SplashScreen.preventAutoHideAsync() 和 SplashScren.hideAsync()。https://docs.expo.dev/versions/latest/sdk/splash-screen/ . 所以我做了并按照提供的链接进行操作。

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

我是否必须将此函数传递给根视图,或者有没有办法将此函数传递给我的提供者/导航容器之一?还是有其他修复方法?

//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;
  }
};

谢谢。

【问题讨论】:

  • Hey Bert - 遇到同样的问题,哈哈,刚刚进入 45 世博会。我刚刚放弃了整个回调。我得到“SplashScreen.show 已经被调用”,只是将真正的回报隐藏在 if isReady 后面。 - 我希望其他人有更好的贡献哈哈
  • 标记的答案对我有用 :)

标签: react-native expo react-navigation


【解决方案1】:

您可以尝试使用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>
 );
}

【讨论】:

  • 这似乎成功了!谢谢。
  • 奇怪,它确实删除了加载,但是应用程序根本没有加载和/或崩溃 x)(不会发生在 expo-app-loading 中)
【解决方案2】:

由于问题使用NavigationContainer,我认为最好的方法是函数返回:

return (
...
  <NavigationContainer
    theme={scheme === "dark" ? darkTheme : lightTheme}
    ref={navigationRef}
    onReady={onLayoutRootView} // <-- HERE!
  >
     <RootStackScreens on/>
  </NavigationContainer>
...
)

此答案基于this code example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多