Ber*_*cke 16 react-native react-navigation expo
我刚刚升级到 Expo SDK 45,收到一条警告:“expo-app-loading 已被弃用,取而代之的是 expo-splash-screen:请使用 SplashScreen.preventAutoHideAsync() 和 SplashScren.hideAsync()。https:// docs。 expo.dev/versions/latest/sdk/splash-screen/ .所以我这样做了并按照提供的链接进行操作。
我现在遇到的问题是,在示例中,他们在根视图的 onLayOut 上调用 onLayOutRootView 。现在我正在使用反应导航,因此我的根视图嵌套在我的应用程序中相当深。
我是否必须将此函数传递到根视图,或者是否有办法将此函数传递到我的提供程序/导航容器之一?或者有其他修复吗?
//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;
}
};
Run Code Online (Sandbox Code Playgroud)
谢谢。
RdC*_*965 20
正如问题所使用的NavigationContainer,我认为最好的方法是遵循expo 的说明,但修改函数的“返回”以传递onLayoutRootView给 theNavigationContainer而不是 a View,如下所示:
return (
...
<NavigationContainer
theme={scheme === "dark" ? darkTheme : lightTheme}
ref={navigationRef}
onReady={onLayoutRootView} // <-- HERE!
>
<RootStackScreens on/>
</NavigationContainer>
...
)
Run Code Online (Sandbox Code Playgroud)
这个答案基于这个代码示例。
小智 17
您可以尝试将所有内容包装在Viewwith中flex:1。
像这样的东西:
...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>
);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
只需调用 set NavigationContainer onReady 到 onLayoutRootView
<NavigationContainer onReady={onLayoutRootView}>
<NavigatorDrawer />
</NavigationContainer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6825 次 |
| 最近记录: |