博览会升级到SDK 34后Font.loadAsync不起作用

时间:2019-09-17 11:07:15

标签: reactjs sdk expo

运行应用程序时出现以下错误。

Warning: React.createElement: type is invalid -- expected a string (for built-in components)
 or a class/function (for composite components) but got: %s.%s%s, undefined, 
 You likely forgot to export your component from the file it's defined in,
 or you might have mixed up default and named imports.

在更新SDK之后,我将字体的导入更改为 import * as Font from 'expo-font'

并更改了相关的package.json版本

"expo": "^34.0.3",
"expo-font": "^6.0.1",

删除了package-lock.json和node_modules并执行了npm i,该错误仍然存​​在。

我如何实现loadAsync

import React, { Component } from 'react'
import * as Font from 'expo-font'
import { AppLoading } from 'expo'
import App from './src/components/App'

export default class App extends Component {
    constructor(props) {
        super(props)
        this.state = {
            isLoadingComplete: false,
        }
    }

    render() {
        console.log("this.state", this.state.isLoadingComplete);
        if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen)
            return <AppLoading
                startAsync={this._loadResourcesAsync}
                onError={this._handleLoadingError}
                onFinish={this._handleFinishLoading}
            />
        else
            return <App />
    }

    _loadResourcesAsync = async () => {
        return Promise.all([
            Font.loadAsync({
                'gt-medium': require('./src/assets/fonts/gt/GT-Walsheim-Pro-Medium.ttf'),
            }),

        ])
    }

    _handleLoadingError = error => {
        console.warn(error)
    }

    _handleFinishLoading = () => {
        this.setState({
            isLoadingComplete: true
        })
    }
}

0 个答案:

没有答案