React native 使用db模型数据更新ListView

React native 使用db模型数据更新ListView,react-native,React Native,我想使用使用db模型创建的数据生成视图: 但问题是:没有任何附加内容,我的视图保持为空。 1)db(for loop)的大小等于0(但在mydb.bills.get_all()之后必须是8)。 构造函数 constructor(props) { super(props); var bdd = []; const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.state

我想使用使用db模型创建的数据生成视图:

但问题是:没有任何附加内容,我的视图保持为空。 1)db(for loop)的大小等于0(但在mydb.bills.get_all()之后必须是8)。

构造函数

constructor(props) {
  super(props);
  var bdd = [];
  const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
  this.state = {
  dataSource: ds.cloneWithRows(bdd)
 };
}
组件安装()


我两天就开始了,请帮帮我^^"这是因为你的
DB.bills.get_all
函数是异步的。当你得到
result
时,你已经计算了
newArray
并设置了状态。在计算
newArray
并克隆数据源之前,你需要等待
结果
。试着把代码放在
DB.bill>之后s、 获取回调函数中的所有

谢谢,这就是问题所在!
componentDidMount() {
console.log("START");

let self = this;
var db = [];

DB.bills.get_all(function(result) {
  db = result;
  console.log(db);
});

  let newArray = self.state.dataSource._dataBlob.s1.slice();
  for(var i = 0; i<Object.keys(db).length ; i++){
    newArray[i] = {
      ...self.state.dataSource[i],
      field: JSON.stringify(db),
    };
    console.log("i : " + i + " db : " + db);
  }

  self.setState({dataSource: self.state.dataSource.cloneWithRows(newArray),
  });
        console.log("DS : ", this.state.dataSource);
  console.log("END");
}
Object {totalrows: 7, autoinc: 8, rows: Object}
autoinc : 8
rows : Object
 1 : Object
  creditor:"M. Alixe"
  month:"Décembre"
  picture:"https://cdn.pbrd.co/images/8YftpB3rQGA5S.png"
  price:"700"
  type:"Maquettes UI"
  year:"2016"
  _id:1
  __proto__:Object
2:Object
3:Object
4:Object
5:Object
6:Object
7:Object
__proto__:Object
totalrows:7
__proto__:Object