簡體   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