【问题标题】:at using Expo, After splash screen, Blink(Flash) with white screen使用Expo时,闪屏后,白屏闪烁(Flash)
【发布时间】:2020-11-11 03:57:43
【问题描述】:

我用 react native navigation 5 制作了 Expo 应用。

please refer my solved question(same install situation).

但在第一次启动时启动后,白屏闪烁(闪烁:约 0.5 秒)。尤其是在 Dark 主题下,我可以很容易地找到这个 bug。

这是 App.tsx

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';

import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation';

export default function App() {
  const isLoadingComplete = useCachedResources();
  const colorScheme = useColorScheme();

 if (!isLoadingComplete) {
    return null;
  } else {
    return (
      <SafeAreaProvider>
        <Navigation colorScheme={colorScheme} />
        <StatusBar style='light'/>
      </SafeAreaProvider>
    );
  }
}

这是导航时的 index.tsx

import { RootStackParamList } from '../types';
import BottomTabNavigator from './BottomTabNavigator';
import LinkingConfiguration from './LinkingConfiguration';

export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeName }) {
  return (
    <NavigationContainer
      linking={LinkingConfiguration}
      theme={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
      <RootNavigator />
    </NavigationContainer>
  );
}

const Stack = createStackNavigator<RootStackParamList>();

function RootNavigator() {
  return (
    <Stack.Navigator screenOptions={{ headerShown: false }}>
      <Stack.Screen name="Root" component={BottomTabNavigator} />
      <Stack.Screen name="NotFound" component={NotFoundScreen} options={{ title: 'Oops!' }} />
    </Stack.Navigator>
  );
}

在 Hook 中,使用CachedResources.ts

import { MaterialIcons } from '@expo/vector-icons';
import * as Font from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';
import * as React from 'react';

export default function useCachedResources() {
  const [isLoadingComplete, setLoadingComplete] = React.useState(false);
  React.useEffect(() => {
    async function loadResourcesAndDataAsync() {
      try {
        SplashScreen.preventAutoHideAsync();

        await Font.loadAsync({
          ...MaterialIcons.font,
          'space-mono': require('../assets/fonts/SpaceMono-Regular.ttf'),
        });
      } catch (e) {
        console.warn(e);
      } finally {
        setLoadingComplete(true);
        SplashScreen.hideAsync();
      }
    }
    loadResourcesAndDataAsync();
  }, []);

  return isLoadingComplete;
}

并使用ColorScheme.ts

import { Appearance, ColorSchemeName } from 'react-native';
import { useEffect, useRef, useState } from 'react';

export default function useColorScheme(delay = 500): NonNullable<ColorSchemeName> {
  const [colorScheme, setColorScheme] = useState(Appearance.getColorScheme());

  let timeout = useRef<NodeJS.Timeout | null>(null).current;

  useEffect(() => {
    Appearance.addChangeListener(onColorSchemeChange);

    return () => {
      resetCurrentTimeout();
      Appearance.removeChangeListener(onColorSchemeChange);
    };
  }, []);

  function onColorSchemeChange(preferences: Appearance.AppearancePreferences) {
    resetCurrentTimeout();

    timeout = setTimeout(() => {
      setColorScheme(preferences.colorScheme);
    }, delay);
  }

  function resetCurrentTimeout() {
    if (timeout) {
      clearTimeout(timeout);
    }
  }

  return colorScheme as NonNullable<ColorSchemeName>;
}

如何解决这个错误?请把手给我。

【问题讨论】:

  • 添加,如果我在 useCachedResources.ts 插入代码(setTimeout(() => {SplashScreen.hideAsync();}, 300),这个错误被隐藏了。但我认为它不是基本解决方案
  • 我也遇到了同样的问题,超时是我找到的唯一解决方案...

标签: react-native navigation expo


【解决方案1】:

我刚刚通过在我的app.json 中指定backgroundColor 以匹配启动屏幕的背景颜色解决了这个问题。

Expo DocsbackgroundColor

(string) - 你的应用的背景颜色,在你的任何 React 视图后面。这也称为根视图背景颜色。 6 个字符长的十六进制颜色字符串,例如,'#000000'。默认为白色:'#ffffff'

【讨论】:

    【解决方案2】:

    他们的其他原因也确保您的导入 {useState,useEffect } 或来自 react 的其他钩子不是 react.development,如果您在博览会上发布您的应用程序或生成 API 或 apk,您将面临这种情况

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2021-03-18
      • 2011-01-06
      • 2017-10-22
      • 2015-03-24
      • 2013-02-17
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多