【问题标题】:React Native: Loading Assets before app is ready threw an exceptionReact Native:在应用程序准备好之前加载资产引发异常
【发布时间】:2018-11-17 06:54:48
【问题描述】:
function cacheImages(images) {
  return images.map(image => {
    if (typeof image === 'string') {
      return Image.prefetch(image);
    } else {
      return Asset.fromModule(image).downloadAsync();
    }
  });
}

function cacheFonts(fonts) {
  return fonts.map(font => Font.loadAsync(font));
}

export default class App extends React.Component {

  constructor (props) {
    super(props);
    this.state = { isReady : false };
  }

  async _loadAssetsAsync () {
      const imageAssets = cacheImages(require('./assets/icon.png'));//['https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png']); 

      const fontAssets = cacheFonts([FontAwesome.font]);

      await Promise.all([...imageAssets, ...fontAssets]);
  }

  render() { 
    if (!this.state.isReady) {
      return (
        <AppLoading
            startAsync={this._loadAssetsAsync}
            onFinish={() => this.setState({ isReady: true })}
            onError={alert('Error loading assets')}/> 
      );
    }

    return (            
      <Provider store={Store}>
        <View style={{ flex:1, width: '100%', height: '100%' }}>          
          <Navigator></Navigator>
          <LoadingModal></LoadingModal>
        </View>
      </Provider>
    );

我尝试在他们的网站上使用 expo 提供的代码在应用程序加载之前预取资产和其他相关图像。

我在渲染 Apploading 元素后收到错误消息。该异常没有显示任何有意义的特定错误。

     AppLoading threw an unexpected error when loading:
cacheImages@http://localhost:19001/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&minify=false&hot=false&assetPlugin=%2FUsers%2Fsimonlam%2FDesktop%2Freact_native%2F

【问题讨论】:

    标签: react-native assets expo preloading


    【解决方案1】:

    去掉AppLoadingonError方法中的alert,改用console.error

    该行存在解析错误。

    【讨论】:

      【解决方案2】:

      你可以使用-

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

      这将使您的代码运行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        • 1970-01-01
        • 2019-01-24
        相关资源
        最近更新 更多