react-native - react native : Loading Assets before app is ready threw an exception

标签 react-native assets expo preloading

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 提供的代码在应用程序加载之前预取 Assets 和其他相关图像。

我在呈现 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

最佳答案

移除 AppLoading onError 方法中的警报并使用 console.error 代替,

该行存在解析错误。

关于react-native - react native : Loading Assets before app is ready threw an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348973/

相关文章:

java - 如何正确关闭文件到字符串的输入流? (IOUtils FileUtils)

ruby-on-rails - 抽佣 Assets :precompile fails all of a sudden

react-native - 在react-native中设置环境变量?

javascript - 如何在React Native中调用本地文件作为Webview的源

reactjs - React Native ScrollView/FlatList 不滚动

android - 如何使用 ActionScript 3 访问 APK 文件中的资源? (土坯空气)

javascript - React Native FlatList 在添加新数据时重新渲染已经渲染的项目

react-native - Expo 在生产版本中未找到 "main"应用程序?

javascript - 如何在胜利 native 的面积图中制作可拖动光标

ios - 如何测试使用 Jest 导入自定义 native 模块的 React Native 组件?