简体   繁体   English

我无法在我的 react native 上使用 apploading。 它不能正常工作

[英]I can't use the apploading on my react native. It wont work properly

I keep getting this error: error screenshot我不断收到此错误:错误截图

Here is the code that im using to import my custom google fonts:这是我用来导入我的自定义谷歌 fonts 的代码:

import React, { useState } from "react";
import Home from "./screens/home";
import { View } from "react-native";
import * as Font from "expo-font";
import { AppLoading } from "expo";

const getFonts = () =>
  Font.loadAsync({
    "poppins-regular": require("./assets/fonts/Poppins-Regular.ttf"),
    "poppins-bold": require("./assets/fonts/Poppins-Bold.ttf"),
  });

export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);
  if (fontsLoaded) {
    return <Home />;
  } else {
    return (
      <AppLoading startAsync={getFonts} onFinish={() => setFontsLoaded(true)} />
    );
  }
}

Here is my Json Package:这是我的 Json Package:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "expo": "~40.0.0",
    "expo-splash-screen": "^0.8.1",
    "expo-status-bar": "~1.0.3",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
    "react-native-web": "~0.13.12"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0"
  },
  "private": true
}

I can't seem to find the error or fix it.我似乎无法找到错误或修复它。 I did this with another project before and it worked.我之前在另一个项目中做过这个,它奏效了。 The getting the font info is exactly the same.获取字体信息是完全一样的。 The only thing i can think of is maybe my packages are outdated atm?我唯一能想到的可能是我的包裹已经过时了? but Im not sure which package to even update.但我不确定要更新哪个 package。

Can someone please help?有人可以帮忙吗?

I can see from your package.json that you are at expo SDK 40 (latest release as of now)我可以从您的 package.json 看到您在 expo ZF20E3C5E54C0AB3D37AZ5D460Fst3

AppLoading has been extracted from the expo package in SDK 40 AppLoading 已从 SDK 40 中的 expo package 中提取

If you want to use this component, you should run expo install expo-app-loading and import AppLoading from its own package: import AppLoading from 'expo-app-loading';.如果你想使用这个组件,你应该运行 expo install expo-app-loading and import AppLoading from its own package: import AppLoading from 'expo-app-loading';。 This is part of an ongoing effort to make the expo package as lightweight as possible.这是使 expo package 尽可能轻量化的持续努力的一部分。

Check the expo blog here.此处查看 expo 博客。

Steps:脚步:
STEP 1: expo install expo-app-loading第 1 步: expo install expo-app-loading

STEP 2:第2步:

replace import { AppLoading } from "expo";替换import { AppLoading } from "expo";

by import AppLoading from 'expo-app-loading';通过import AppLoading from 'expo-app-loading';

STEP 3: Restart your expo dev server (expo start)第 3 步:重新启动您的 expo 开发服务器(展会开始)

My problem was that the first time reloading the app (with expo), the fonts were not loading.我的问题是第一次重新加载应用程序(使用 expo),fonts 没有加载。

After deleting, rewriting and saving the fontFamily inside of my StyleSheet , the text was being styled with the right font, as the app updates automatically every time you save a file.在我的StyleSheet中删除、重写和保存 fontFamily 之后,文本的样式使用正确的字体,因为每次保存文件时应用程序都会自动更新。 I just couldn't make it work at the first load.我只是无法让它在第一次加载时工作。

I solved the issue by putting a return in front of the Font.loadAsync() function:我通过在Font.loadAsync() function 前面添加一个 return 解决了这个问题:

 const fetchFonts = () => { return Font.loadAsync({ // <- add return here "open-sans": require("./assets/fonts/OpenSans-Regular.ttf"), "open-sans-bold": require("./assets/fonts/OpenSans-Bold.ttf"), }); };

Now the AppLoading's startAsync gets the promise correctly and the fonts are loaded right after loading the app, without errors现在 AppLoading 的startAsync正确获取 promise 并且 fonts 在加载应用程序后立即加载,没有错误

 const [isDataLoaded, setIsDataLoaded] = useState(false); if (.isDataLoaded) { return ( <AppLoading startAsync={fetchFonts} onFinish={() => setIsDataLoaded(true)} onError={(err) => console;log(err)} /> ); }

you have to add the return keyword infront of font.loadAsync您必须在 font.loadAsync 前面添加return关键字

Just a heads up, as of today, using startAsync method is depracted in expo-app-loading , you can use useEffect instead and run your function as you would like and handle all the errors there,只是提醒一下,截至今天,在expo-app-loading中已弃用 startAsync 方法,您可以改用useEffect并根据需要运行 function 并处理那里的所有错误,

  useEffect(() => {
      const getAsyncData = async () => {
           // Do my async request
           setIsReady(true); 
      }
      getAsyncData ();
   }, [])

Assuming you have something like const [isReady, setIsReady] = useState(false);假设你有类似const [isReady, setIsReady] = useState(false); in your code在你的代码中

for me the following steps result:对我来说,结果如下:
STEP 1: stop device STEP 1:停止装置
STEP 2: expo install expo-app-loading第 2 步:expo 安装 expo-app-loading
STEP 3: add the next line: import AppLoading from 'expo-app-loading'; STEP 3:添加下一行:import AppLoading from 'expo-app-loading';
STEP 4:第4步:
if (!isDataLoaded) {如果(!isDataLoaded){
return (返回 (
<AppLoading <应用加载
startAsync={fetchFonts} startAsync={fetchFonts}
onFinish={() => setIsDataLoaded(true)} onFinish={() => setIsDataLoaded(true)}
onError={(err) => console.log(err)} onError={(err) => console.log(err)}
/> />
); );
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应原生。 在我的代码中有一些错误 - react native. have some error in my code 反应原生。 如何在没有 onPress() 的情况下导航到下一个屏幕 - React Native. How can I navigate to the next screen with out onPress() 由于使用 React Native 的虚拟键盘,复选框被选中但没有注册值。 我怎样才能解决这个问题? - Checkboxes getting checked but no value registered because of virtual keyboard using React Native. How can I fix this? 如何在React Native中将组件正确导入导航器? - How can I properly import a Component into my Navigator in React Native? SetState 无法正常工作 React Native - SetState doesn't work properly React Native 为什么我设置的时间间隔不能在 React Native 应用程序中使用? - why wont my set interval work in react native app? 无法在React中获取我的表单以正常工作 - Can't get my form in React to work properly react-native 应用程序中的 Pressables 无法使用 - Pressables in react-native app wont't work 如果我使用 await,React-Native 应用程序将无法运行 - React-Native app doesn't work if i use await 反应本地。 我安装库本机反应得到错误(失败) - react native. I install library react native an got error (failed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM