反应本机:TypeError:null 不是对象(评估“SplashScreen.preventAutoHide”)

use*_*835 8 ios react-native expo

在我使用expo eject. 我退出它是因为我现在打算构建应用程序并将其发布到 ios 应用程序商店。一旦我尝试在弹出的应用程序react-native run-ios被弹出后使用它启动弹出的应用程序,我就会收到以下异常。

请有人帮助了解导致此问题的原因以及如何解决?

反应本机版本如下:

react-native-cli: 2.0.1
react-native: 0.61.5
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

TypeError: null is not an object (evaluating 'SplashScreen.preventAutoHide')

This error is located at:
    in AppLoading (at AppLoading.js:52)
    in AppLoading (at App.js:464)
    in App (at renderApplication.js:40)
    in RCTView (at AppContainer.js:101)
    in RCTView (at AppContainer.js:119)
    in AppContainer (at renderApplication.js:39)

preventAutoHide
    SplashScreen.js:4:21
AppLoading#constructor
    AppLoadingNativeWrapper.js:6:8
renderRoot
    [native code]:0
runRootCallback
    [native code]:0
renderApplication
    renderApplication.js:52:52
runnables.appKey.run
    AppRegistry.js:116:10
runApplication
    AppRegistry.js:197:26
callFunctionReturnFlushedQueue
    [native code]:0
Run Code Online (Sandbox Code Playgroud)

And*_*dru 11

AppLoading组件是不是在裸露的工作流程可用。正如@gaurav-roy 所说,您必须重构代码。

  1. 安装expo-splash-screennpm install expo-splash-screen

  2. 为您的 Android 和 iOS 项目添加启动画面。运行npm run expo-splash-screen --help并按照此 CLI 工具的说明进行操作。(由于存在错误,-p "ios"如果它在运行后仅添加适用于 Android 的 SplashScreen,则您可能必须再次使用该标志运行该命令。

  3. App.tsx以与本示例类似的方式更改内部代码。

    如果您正在使用钩子,您可能希望添加一个useEffect 带有空依赖项列表的钩子,该列表运行异步函数。这是如何完成的示例:

const App = (props: Props) => {
  const [isLoadingComplete, setLoadingComplete] = useState(false);
  
  const init = async () => {
  try {
    // Keep on showing the SlashScreen
    await SplashScreen.preventAutoHideAsync();
    await loadResourcesAsync();
  } catch (e) {
    console.warn(e);
  } finally {
    setLoadingComplete(true);
    // Hiding the SplashScreen
    await SplashScreen.hideAsync();
  }
  
  useEffect(() => {
    init();
  }, []);

  const renderApp = () => {
    if (!isLoadingComplete && !props.skipLoadingScreen) {
      return null;
    }

    return (
      <Main />
    );
  };
  return <StoreProvider>{renderApp()}</StoreProvider>;
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*Roy 5

从 docs 可以看出,SplashScreen 是用于博览会应用程序的内置 api,并且由于您弹出它,因此它会引发错误,因为它无法使用。

您可以在 docs expo splashscreen 中看到这一点。

首先你应该下载 npm i expo-splash-screen

然后将您的导入语句更改为:

import * as SplashScreen from 'expo-splash-screen';
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。随意怀疑