【问题标题】:React native error of Check the render method of AppReact Native 错误检查 App 的渲染方法
【发布时间】:2021-08-17 22:10:25
【问题描述】:

我正在制作一个反应原生应用程序,现在正试图放置一个下载的字体,我一直在检查应用程序错误的渲染方法,然后在它下面,它说这个

此错误位于: 在 App 中(由 ExpoRoot 创建) 在 ExpoRoot (在 renderApplication.js:45) 在 RCTView 中(在 View.js:34) 在视图中(在 AppContainer.js:106) 在 RCTView 中(在 View.js:34) 在视图中(在 AppContainer.js:132) 在 AppContainer 中(在 renderApplication.js:39 处)

我看过类似的问题,但他们的解决方案没有解决我的问题,所以我从他们的解决方案中得知这是导入或导出错误,但我的所有东西都正确导入和导出,所以我不知道是什么问题不再是了。这就是我当前的代码的样子

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

const getFonts = () => 
   Font.loadAsync({
    "Alef-Bold": require("./assets/fonts/Alef-Bold.ttf")
  })


export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);

  if(fontsLoaded){
    return (
      <View>
        <Home />
      </View>
    );
  } else {
    return (
      <AppLoading startAsync={getFonts} onFinish={() => setFontsLoaded(true)} />
    )
  }
}

这是 Home 组件

    import React from 'react'
import {StyleSheet, Text, View } from 'react-native'

function Home() {
    return (
        <View style={styles.container}>
            <Text style={styles.titleText}>Welcome to the home page</Text>
        </View>
    )
}

const styles = StyleSheet.create({
    container: {
        padding: 50,
    },
    titleText: {
        fontFamily: "Alef-Bold",
    }
})

export default Home

请问我该如何解决呢?

【问题讨论】:

  • 你能发布你得到的确切错误吗?
  • 这是第一个错误错误:元素类型无效:期望字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。检查App的渲染方法。我没有足够的字符来输出其余的错误,所以我更新了问题
  • 你的AppLoading导入有误,你要这样导入import AppLoading from 'expo-app-loading';
  • @KartikeyVaish 这个错误给了我这个错误 undefined Unable to resolve module expo-app-loading from C:\Users\lenovo\Desktop\projekten\gamezone\App.js: expo-app-loading can在项目中找不到。如果您确定该模块存在,请尝试以下步骤: 1. 清除 watchman 手表:watchman watch-del-all 2. 删除 node_modules 并运行 yarn install 3. 重置 Metro 的缓存:yarn start --reset-cache 4. 删除缓存: rm -rf /tmp/metro-* 3 |从'expo-font'导入*作为字体; 4 |从'./screens/Home'导入主页; > 5 |从'expo-app-loading'导入AppLoading
  • 你必须像这样安装应用程序加载expo install expo-app-loading看我的回答

标签: reactjs react-native function fonts expo


【解决方案1】:

您加载字体的方式是错误的,您的导入也是如此。要更正此问题,只需按照以下步骤操作

Click here to see Working Example

首先像这样安装AppLoading

expo install expo-app-loading

在您的App.js 所在的位置创建一个名为hooks 的文件夹。

hooks 文件夹中创建一个名为useFonts.js 的文件,粘贴此代码

useFonts.js

import * as Font from 'expo-font';

const useFonts = async () => {
  await Font.loadAsync({
    'Alef-Bold': require('../assets/fonts/Alef-Bold.ttf'),
  });
};

export default useFonts;

然后在您的App.js 中粘贴此代码

import React, { useState } from 'react';
import { View } from 'react-native';
import AppLoading from 'expo-app-loading';

import Home from './screens/Home';
import useFonts from './hooks/useFonts';

export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);

  const LoadFonts = async () => {
    await useFonts(); // We have to await this call here
  };

  if (!fontsLoaded) {
    return (
      <AppLoading
        startAsync={LoadFonts}
        onFinish={() => setFontsLoaded(true)}
        onError={(error) => console.log(error)}
      />
    );
  }

  return (
    <View>
      <Home />
    </View>
  );
}

你的Home.js 应该是这样的

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

function Home() {
  return (
    <View style={styles.container}>
      <Text style={styles.titleText}>Welcome to the home page</Text>
      <Text style={styles.normalText}>This text is in normal font</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    padding: 50,
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    color: 'red',
  },
  titleText: {
    fontFamily: 'Alef-Bold',
    fontSize: 30,
  },
  normalText: {
    fontSize: 30,
  },
});

export default Home;

通过这种加载字体的方式,您的App.js 将变得整洁干净。我在所有项目中都使用这种方式

【讨论】:

  • 嘿,伙计,现在我收到此错误这些文件都不存在:*hooks\useFonts\index(.native|.android.ts|.native.ts|.ts|.android.tsx| .native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json) 3 |从'expo-app-loading'导入AppLoading; 4 |从'./screens/Home'导入主页; > 5 |从'./hooks/useFonts'导入useFonts;这很奇怪,因为我已经创建了 hooks 文件夹和 useFonts 文件,并将你的代码完全按照你写的那样放在那里
  • 检查所有文件夹和文件名是否正确。还要确保在您的 App.js 所在的位置创建 hooks 文件夹。检查您的 Home 组件的文件名是 Home.js 还是 HomeScreen.js 检查我提供的链接上的工作实现
  • 非常感谢兄弟的宝贵时间 非常感谢它现在可以正常工作,但还有一个问题,当我添加另一种字体时,它会抛出此错误 fontFamily "Airstream" 不是系统字体,还没有通过 Font.loadAsync 加载。 - 如果您打算使用系统字体,请确保您输入了正确的名称并且您的设备操作系统支持该名称。 - 如果这是自定义字体,请务必使用 Font.loadAsync 加载它。这是你可以在我保存的博览会上查看的更新版本snack.expo.io/bnTQsONgl
  • 您必须在字体文件夹中上传Airstream.ttf 才能使用
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多