繁体   English   中英

在react-native应用中显示Firebase数据

[英]show firebase data in react-native app

我正在尝试在react-native上构建我的第一个应用程序,但是我有问题,我想显示我的Firebase数据,并且在重新加载应用程序时不会删除插入到Firebase的数据。 多数民众赞成在我的渲染:

 let tags = this.state.tagArray.map((val, key) => {
  return <TagContainer key={key} keyval={key} val={val}
    deleteMethod={() => this.deleteTag(key)} />
});

这是我的firebase配置:

this.itemRef=this.getRef().child('tags');
getRef(){
  return firebaseApp.database().ref();
}

getItems(itemRef){
  itemRef.on('value',(snap)=>{
    let items=[];
    snap.forEach((child) => {
      items.push({
        title:child.val().title,
        _key:child.key
      });   
    });
 Array.prototype.push.apply(this.state.tagArray, items);
  this.setState({ tagArray: this.state.tagArray })
  });

这是我的addtag函数,可在其中设置我的tagArray:

addTag() {
  if (this.state.tagText) {
    this.state.tagArray.push({
      'tag': this.state.tagText
    });
    this.setState({ tagArray: this.state.tagArray })
    this.setState({ tagText: '' })
    this.itemRef.push({title:this.state.tagText});
  }
}

您不应该将数据推入状态对象。 不使用setState()不允许状态更改。

addTag() {
  if (this.state.tagText) {

      let tagArray = [...this.state.tagArray,{'tag': this.state.tagText}]
      this.setState({ tagArray: tagArray,tagText: '' }, ()=>{
          this.itemRef.push({title:this.state.tagText});
      })
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM