You need to enable JavaScript to run this app. @expo/vector-icons图标显示问号的突发异常排查求助
优惠活动
大模型
产品
解决方案
定价
更多

@expo/vector-icons图标显示问号的突发异常排查求助

问题背景
  • 项目基于Expo SDK 40开发,集成@expo/vector-icons实现导航栏矢量图标渲染,当前所有图标均显示为问号占位
  • 已执行基础排查操作:清除Metro缓存、删除node_modules目录、强制清空npm缓存重装依赖、运行expo r -c启动项目,异常无改善
  • 问题为突发出现,此前项目运行状态完全正常
核心依赖配置
{
  "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/vector-icons": "^12.0.3",
    "@react-native-community/masked-view": "^0.1.10",
    "expo": "~40.0.0",
    "expo-app-loading": "^1.0.1",
    "expo-font": "^9.0.0",
    "expo-linear-gradient": "^8.4.0",
    "expo-status-bar": "~1.0.3",
    "moment": "^2.29.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "Expo SDK 40 官方适配版本",
    "react-native-gesture-handler": "~1.8.0",
    "react-native-reanimated": "~1.13.0",
    "react-native-screens": "^2.16.1",
    "react-native-vector-icons": "^8.0.0",
    "react-native-web": "~0.13.12",
    "react-navigation": "^4.4.3",
    "react-navigation-drawer": "^2.6.0",
    "react-navigation-header-buttons": "^6.0.0",
    "react-navigation-stack": "^2.10.2",
    "react-redux": "^7.2.2",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0",
    "redux-devtools-extension": "^2.13.8"
  },
  "private": true
}
业务实现代码

自定义导航栏按钮组件

import React from 'react';
import { Platform } from 'react-native';
import { HeaderButton } from 'react-navigation-header-buttons';
import { Ionicons } from '@expo/vector-icons';

import Colors from '../../constants/Colors';

const CustomHeaderButton = props => {
    return <HeaderButton 
        {...props} 
        IconComponent={Ionicons} 
        iconSize={23} 
        color={Platform.OS === 'android' ? 'white' : Colors.primary}
    />
};

export default CustomHeaderButton;

商品概览页导航栏配置

import React, { useEffect, useState, useCallback } from 'react';
import { View, FlatList, StyleSheet, Platform, Button, ActivityIndicator, Text } from 'react-native';
import { useSelector, useDispatch } from 'react-redux';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import * as cartActions from '../../store/actions/cart';
import ProductItem from '../../components/shop/ProductItem';
import HeaderButton from '../../components/UI/HeaderButton';
import Colors from '../../constants/Colors';
import * as productsActions from '../../store/actions/products';

const ProductsOverviewScreen = props => {
  // 页面业务逻辑实现
}

ProductsOverviewScreen.navigationOptions = navData => {
  return {
    headerTitle: 'All Products',
    headerLeft: () => (
      <HeaderButtons HeaderButtonComponent={HeaderButton}>
        <Item
          title="Menu"
          iconName={Platform.OS === 'android' ? 'md-menu' : 'ios-menu'}
          onPress={() => {
            navData.navigation.toggleDrawer();
          }}
        />
      </HeaderButtons>
    ),
    headerRight:() => (
      <HeaderButtons HeaderButtonComponent={HeaderButton}>
        <Item
          title="Cart"
          iconName={Platform.OS === 'android' ? 'md-cart' : 'ios-cart'}
          onPress={() => {
            navData.navigation.navigate('Cart');
          }}
        />
      </HeaderButtons>
    )
  };
};

const styles = StyleSheet.create({
  centered: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
})

export default ProductsOverviewScreen;
可落地的排查解决路径
  • 清除依赖冲突:项目依赖中同时安装了社区版react-native-vector-icons@^8.0.0,与Expo内置的@expo/vector-icons存在字体加载路径冲突,会导致开发服务无法正确解析矢量图标字体文件。执行npm uninstall react-native-vector-icons卸载该非必要依赖,@expo/vector-icons已完整覆盖该社区库的全部使用能力,无需重复安装。
  • 修正版本匹配:原配置中@expo/vector-icons@12.0.3为已知存在字体加载bug的版本,与Expo SDK 40适配的稳定版本为12.0.5,执行npm install @expo/vector-icons@12.0.5完成版本对齐。
  • 补全字体预加载逻辑:Expo SDK 40不会自动预加载@expo/vector-icons的字体资源,需要在应用入口文件的根组件中显式加载字体,避免图标渲染时字体未完成加载触发问号占位,示例实现:
    import { useFonts } from 'expo-font';
    import { Ionicons } from '@expo/vector-icons';
    import AppLoading from 'expo-app-loading';
    
    // 在根组件中挂载字体加载逻辑
    const [fontsLoaded] = useFonts({
      ...Ionicons.font
    });
    
    // 字体未加载完成前保持启动页
    if (!fontsLoaded) return <AppLoading />;
    
  • 重新启动开发服务:完成上述调整后,执行npx expo start --clear启动服务,重新打开应用预览即可正常渲染图标。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.07 15:54:08