编译到android并通过Store下载后,我发现错误:

"TypeError: Invalid attempt to spread non-iterable instance"

但使用"react native run android"不会产生任何错误消息,因此我找不到调试它的好方法.

fetch(url)
  .then(response => response.json())
  .then(response => {
    if (this._mounted) {
      // let dataSource = this.state.articlesDataSource.cloneWithRows(response.data || [])
      //var rowCount = dataSource.getRowCount();
      var rowCount = Object.keys(response.data).length;

      if (refresh == true) {
        prevData = {};
      } else {
        prevData = this.state.articlesData;
      }
      if (propSearch == "" || propSearch == null) {
        newArticlesData = [...prevData, ...response.data];
      } else {
        newArticlesData = response.data;
      }
      if (response.meta.next_page != null) {
        var rowCount = true;
      } else {
        var rowCount = Object.keys(newArticlesData).length;
      }
      if (this._mounted) {
        this.setState({
          isLoading: false,
          //articlesDataSource: this.state.articlesDataSource.cloneWithRows(response.data),
          articlesData: newArticlesData,
          nextPage: response.meta.next_page,
          fetchUrl: url,
          rowCount: rowCount
        });
      }
    }
  })
  .catch(error => {
    this.setState({
      errorFound: true,
      errorMassage: error,
      isLoading: false
    }); 
});

谢谢你的帮助.

推荐答案

这是因为这是一个运行时错误,而不是"编译时"错误.

是否有与错误相关联的行号?基于关于排列运算符的问题,我假设是这条线:newArticlesData=[...prevData,...response.data].我想你的prevData是可以接受的,但是你的回复数据呢?试试newArticlesData=[...prevData, response.data]

下面是一个无效扩展运算符使用的示例:

function trySpread(object) {
  let array;
  try {
    array = [...object];
    console.log('No error', array);
  } catch(error) {
    console.log('error', error);
  }
}

// error
trySpread({});
trySpread({foo: 'bar'});
trySpread(4);

// no error
trySpread([]);
trySpread(['foobar']);
trySpread('foobar');

React-native相关问答推荐

出现错误:使用 process(css).then(cb) 来使用异步插件

将值从一个 js 文件传递​​到 react native 中的另一个 js 文件:react native

任务:react-native-async-storage_async-storage:generateDebugRFile FAILED

有人可以帮我实现 Font.loadAsync

需要以前的props和state回调的静态 getDerivedStateFromProps?

TextInput 防止 ScrollView 滚动

withNavigation 只能用于导航器的视图层次 struct

React Native - ScrollView 中的 ListView 分页?

React-Native 按钮居中对齐

使用 react-native 设置 Select 器的高度

TouchableHighlight onPress 不起作用

java.lang.ClassNotFoundException:在路径上找不到类.MainActivity:DexPathList react-native

如果我有 AngularJS 基础知识,怎么学习Thinking in ReactJS呢?

在 react-native 中设置动态 'initialRouteName'

如何在自定义组件上使用 onPress?

React-native google 登录给开发人员错误

如何为 TouchableOpacity 组件创建禁用样式?

如何将 jest 测试移动到 /test 文件夹并让它们运行?

React Native:并排对齐两个 TextInput

React Native 项目,有时使用 expo start 获得连接 ECONNREFUSED 127.0.0.1:19001