Expo Router栈导航出现白闪:设置背景色无效的技术求助
解决Expo Router栈导航页面切换白闪问题
方法1:配置Expo Router全局背景色
在app/_layout.js(或app/_layout.tsx)中,给Stack组件设置全局屏幕样式,同时给根容器添加背景色:
import { Stack } from 'expo-router'; import { View } from 'react-native'; import { Slot } from 'expo-router'; export default function RootLayout() { return ( <View style={{ flex: 1, backgroundColor: '#000000' }}> <Stack screenOptions={{ headerStyle: { backgroundColor: '#000000' }, contentStyle: { backgroundColor: '#000000' } }} > {/* 嵌套的路由页面 */} </Stack> </View> ); }
方法2:关闭页面过渡动画(临时缓解)
若白闪由过渡动画触发,可全局或单个页面关闭动画:
// 全局关闭 <Stack screenOptions={{ animation: 'none' }} /> // 单个页面关闭 <Stack.Screen name="your-target-screen" options={{ animation: 'none' }} />
方法3:完善Splash Screen配置
确保启动屏与应用背景色完全一致,修改app.json:
{ "expo": { "backgroundColor": "#000000", "splash": { "backgroundColor": "#000000", "resizeMode": "contain", "image": "./assets/splash.png" } } }
方法4:用expo-splash-screen控制启动屏隐藏时机
- 安装依赖:
npx expo install expo-splash-screen
- 在根组件中添加逻辑,确保应用就绪后再隐藏启动屏:
import * as SplashScreen from 'expo-splash-screen'; import { useEffect, useState } from 'react'; import { View } from 'react-native'; import { Slot } from 'expo-router'; // 阻止启动屏自动隐藏 SplashScreen.preventAutoHideAsync(); export default function App() { const [appReady, setAppReady] = useState(false); useEffect(() => { async function prepareApp() { try { // 这里可添加初始化逻辑(如加载资源) await new Promise(resolve => setTimeout(resolve, 300)); } catch (err) { console.warn(err); } finally { setAppReady(true); await SplashScreen.hideAsync(); } } prepareApp(); }, []); if (!appReady) return null; return ( <View style={{ flex: 1, backgroundColor: '#000000' }}> <Slot /> </View> ); }
内容的提问来源于stack exchange,提问作者Timothée Billiet
相关产品推荐
相关产品推荐

