如何解决React Native Expo SDK40中Font全局API弃用警告
Hey Marco, let's squash that deprecation warning for good! The root issue here is that you're relying on the global Expo.Font object instead of the module you already imported. Here's exactly how to fix it:
Step-by-Step Fix
- You already have the correct import for
expo-fontat the top of your file—great start:import * as Font from 'expo-font'; - Now just replace the global
Expo.Font.loadAsynccall in yourcomponentDidMountmethod with the importedFontmodule you already have. That's the only change you need!
Modified Full Code
Here's your updated component with the fix applied:
// RNRF logic here import React, { Component } from 'react'; import { StyleSheet, TouchableOpacity, Text, View } from 'react-native' import AppLoading from 'expo-app-loading'; import * as Font from 'expo-font'; import { Ionicons } from '@expo/vector-icons'; import { Container, Root} from 'native-base'; import Constants from 'expo-constants'; export default class App extends React.Component { constructor(props) { super(props); this.state = { isReady: false, }; } async componentDidMount() { // Use the imported Font module instead of global Expo.Font await Font.loadAsync({ 'Roboto': require('native-base/Fonts/Roboto.ttf'), 'Roboto_medium': require('native-base/Fonts/Roboto_medium.ttf'), ...Ionicons.font, }); this.setState({ isReady: true }); } render(){ if (!this.state.isReady) { return <AppLoading />; } return( <View> <Text>Hello</Text> </View> ); } }
Why This Works
Starting with Expo SDK 41, the global __expo and Expo objects are being phased out entirely. All Expo APIs (like expo-font) now require explicit module imports, which follows modern ES module best practices and ensures your app stays compatible with future Expo updates.
This change will fully resolve the warning without needing to ignore it—no more noisy logs cluttering up your dev environment!
内容的提问来源于stack exchange,提问作者Marco Martin

