简体   繁体   中英

Error while loading custom fonts in expo project

I am trying to load fonts in my expo managed project, I've followed the example given at https://docs.expo.io/versions/latest/sdk/font/ but it shows me this error "Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null."

Here's my code for App.js

import React from "react";
import { View } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { useFonts } from "expo-font";
import { AppLoading } from "expo";

import LoginScreen from "./assets/Screens/LoginScreen";
import RegisterScreen from "./assets/Screens/RegisterScreen";

const Stack = createStackNavigator();

const App = () => {
  const [isFontLoaded] = useFonts({
    nimbusBold: require("./assets/fonts/NimbusMonoPS-Bold.otf"),
    nimbusRegular: require("./assets/fonts/NimbusMonoPS-Regular.otf"),
  });

  if (!isFontLoaded) {
    return AppLoading;
  }

  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName="Login"
        screenOptions={{
          headerShown: false,
        }}
      >
        <Stack.Screen name="Login" component={LoginScreen} />
        <Stack.Screen name="Register" component={RegisterScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;

Does anyone know why is this happening. Thanks for precious time and answer in advance.

Working Example here

Firsly, run npm i expo-font

Then, run npm i @expo-google-fonts/open-sans

Then, run npm i expo-app-loading

Then in your project folder create a file called hooks where your App.js is located.

in hooks folder create a file called useFonts.js and inside that paste this code

import * as Font from 'expo-font';

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

export default useFonts;

Your App.js should look like this

import React, { Component, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import AppLoading from 'expo-app-loading'; // This is must

import useFonts from './hooks/useFonts';

import LoginScreen from './assets/screens/LoginScreen';
import RegisterScreen from './assets/screens/RegisterScreen';

const Stack = createStackNavigator();

export default function App(props) {
  const [IsReady, SetIsReady] = useState(false);

  const LoadFontsAndRestoreToken = async () => {
    await useFonts();
  };

  if (!IsReady) {
    return (
      <AppLoading
        startAsync={LoadFontsAndRestoreToken}
        onFinish={() => SetIsReady(true)}
        onError={() => {}}
      />
    );
  }

  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName="Login"
        screenOptions={{
          headerShown: false,
        }}>
        <Stack.Screen name="Login" component={LoginScreen} />
        <Stack.Screen name="Register" component={RegisterScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

Your LoginScreen.js should look like this

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

function LoginScreen(props) {
  return (
    <View style={styles.container}>
      <Text style={{ fontFamily: 'Montserrat', fontSize: 30 }}>
        LoginScreen with fontFamily
      </Text>
      <Text style={{ fontSize: 30 }}>LoginScreen without fontFamily</Text>
    </View>
  );
}

export default LoginScreen;

const styles = StyleSheet.create({
  container: {},
});

This is the best way to load fonts and I do it like this in all my projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM