You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo自定义字体加载失败,寻求技术解决方法

Hey there, I see exactly where the issue is—your app is trying to render the login screen before the custom font finishes loading. Let's fix this step by step:

Root Cause

You're loading the font asynchronously in componentDidMount, but your render method immediately returns the RootStack without waiting for the fontLoaded state to flip to true. This means the login screen renders while the font is still being fetched, hence the error.

Fix Steps

1. Add a loading state check in your App component

Wait until the font is fully loaded before rendering your navigation stack. You can show a simple loading indicator in the meantime:

2. Verify font path and naming

Double-check that your font file exists at ./src/assets/fonts/OpenSans-Bold.ttf, and that the filename matches exactly (case sensitivity matters on some systems—don't mix OpenSans-Bold.ttf with opensans-bold.ttf).

Modified App.js Code
import React from 'react'; 
import { View, ActivityIndicator, Platform, NativeModules } from 'react-native'; 
import { Expo } from 'expo'; 
import { StackNavigator } from 'react-navigation'; 
import LoginScreen from './src/views/LoginScreen'; 
import AuthenticatedScreen from './src/views/AuthenticatedScreen'; 
import './ReactotronConfig' 

const { StatusBarManager } = NativeModules; 

const RootStack = StackNavigator( 
  { 
    Login: { screen: LoginScreen }, 
    Authenticated: { screen: AuthenticatedScreen } 
  }, 
  { initialRouteName: 'Login'} 
); 

export default class App extends React.Component { 
  state = { 
    fontLoaded: false, 
  }; 

  async componentDidMount() { 
    try {
      await Expo.Font.loadAsync({ 
        'open-sans-bold': require('./src/assets/fonts/OpenSans-Bold.ttf'), 
      }); 
      console.log('Custom font loaded successfully!')
      this.setState({ fontLoaded: true }); 
    } catch (error) {
      console.error('Failed to load font:', error);
    }
  } 

  render() { 
    // Show loader until font is ready
    if (!this.state.fontLoaded) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <ActivityIndicator size="large" color="#0000ff" />
        </View>
      );
    }

    return <RootStack />; 
  } 
} 

console.disableYellowBox = true; 
Extra Checks
  • If you're using Expo SDK 38+, Expo.Font has moved to the expo-font package. You'll need to run expo install expo-font, then import it as import * as Font from 'expo-font' and use Font.loadAsync(...) instead.
  • Make sure the font name 'open-sans-bold' in your login screen's Text component matches exactly the key you used in loadAsync (no typos, same casing).

This should resolve the font loading error and let you use your custom font properly!

内容的提问来源于stack exchange,提问作者tmehta2442

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:09