【问题标题】:Updating ListView (With realm) when navigator.popnavigator.pop 时更新 ListView(带领域)
【发布时间】:2017-05-09 12:47:51
【问题描述】:

我几乎完成了我的申请,但它仍然是一个主要问题:

当我使用 navigator.pop 时,我的 ListView 没有更新。

列表视图

<ListView
    enableEmptySections={true}
    style={styles.listView}
    dataSource={this.state.dataSource}
    renderRow={(data) => <Row {...data} navigator={this.props.navigator} />}
    renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator} />}
/>

ListBillScreen.js

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

componentWillUpdate () {
  this.setState = {
  realm: this.props.billsbdd.realm
}
}

componentDidMount() {

  BackHandler.addEventListener('backPress', () =>
    this.popIfExists()
  );

  realms = this.state.realm;

  if(realms == ""){
    src = [];
  }
  else{
    src =  this.state.realm.objects('Bills');
  }

  this.setState({
    dataSource: this.state.dataSource.cloneWithRows(src)
  })

}

AddBillScreen.js

  handlePressCreate() {
  //TODO: Creation of a bill

    let realms = this.state.realm;

    realms.write(() => {
      realms.create('Bills', {
        id: uuid.v4(),
        type: this.state.billType,
        creditor: this.state.billCreditor,
        month: this.state.billMonth,
        year: this.state.billYear,
        price: this.state.billPrice,
      })
    });
    alert("Facture créée ! ");

    this.props.navigator.pop();

}

我希望,当我按下返回按钮时,我的 ListView 正在使用新数据呈现自己。 我每次都必须再次返回并重新连接才能看到我的 ListView 已更新。 我在stackoverlflow上尝试了一些流行的解决方案,但对我来说没有一个是容易的...... 如果有人可以帮助我,那你真是太好了!谢谢大家!

【问题讨论】:

    标签: listview react-native realm


    【解决方案1】:

    我找到了答案!

    我需要添加

    componentWillReceiveProps() {
     realms = this.state.realm;
    
    if(realms == ""){
      src = [];
    }
    else{
      src =  this.state.realm.objects('Bills');
    }
    
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(src)
    })
    }
    

    ListBillScreen.js 中使其工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多