React Native:在应用准备就绪之前加载资产引发异常

时间:2018-11-17 06:54:48

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

我在渲染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

2 个答案:

答案 0 :(得分:2)

尝试在AppLoading onError方法中删除警报并改用console.error,我认为它在解析该行时遇到问题

答案 1 :(得分:0)

您可以使用-

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

这将使您的代码运行