Reactjs 每次在react native中装入组件时,Firebase snapshot.foreach都会运行

Reactjs 每次在react native中装入组件时,Firebase snapshot.foreach都会运行,reactjs,firebase,react-native,Reactjs,Firebase,React Native,我正在尝试用firebase制作react native中的照片时间表。我的意图是,当用户启动应用程序时,他/她必须按下按钮检索数据。这看起来像这样: 鉴于: <TouchableOpacity onPress={() =>this.getMessagesNew(0)}><Text>Haal berichten op</Text></TouchableOpacity> 函数this.getMessagesNew()从firebase获取最

我正在尝试用firebase制作react native中的照片时间表。我的意图是,当用户启动应用程序时,他/她必须按下按钮检索数据。这看起来像这样:

鉴于:

 <TouchableOpacity onPress={() =>this.getMessagesNew(0)}><Text>Haal berichten op</Text></TouchableOpacity>
函数this.getMessagesNew()从firebase获取最后三条消息。这是不费吹灰之力的

但该应用程序还允许用户将照片上传到firebase。上传后,用户被导航回主屏幕。这里出错了,刚刚上传的图片已经显示出来了。虽然这不是目的

我希望用户必须再次按下按钮才能检索新照片

我知道函数的第一部分不是重新加载的,而是从
snapshot.foreach
加载的


是否有任何方法停止此过程,并使其仅在按下按钮时发生?

您正在使用
命令订阅更改。在('value',…)
上这就是为什么每次上载图像时都会同步内容,以便使用
命令从数据库获取一次值。一次('value',…)
然后一切都会按照您的预期进行

我正在重写你的代码

getMessagesNew(key){
  console.log('triggerd');
  this.setState({
      amountVideos: 0,
      downloadedVideos:0
  });

  this.messages = [];
  var orders = firebase.database().ref('messages/'+key+'/posts').orderByChild('post').limitToLast(3);
  orders.once('value', snapshot => { // observe i have changed "on" to "once"

  snapshot.forEach( (order) => {
      console.log('triggerd in loop')
      let state = order.val();
      if(state != null){
        var newelement = {date: state.date, function: state.function,image:state.image,name: state.name,  text: state.text, type:state.type, post: state.post,};
    if(newelement.type == false){
      this.setState({amountVideos: this.state.amountVideos+1})
      this.downloadImage(newelement);
    }else{
      messages.push(newelement);
      if(this.state.amountVideos == 0){
        this.setState({
          loading: false,
          messagesAvailable: true,
          refresh: this.state.refresh+1,
        })
      }else{
        console.log('videos laden');
        this.setState({
          refresh: this.state.refresh+1
        })
      }
       console.log('message local val'+this.messages);
       console.log('message state val'+this.state.messages);

    }
      
  }else{
    this.setState({
      messagesAvailable: false,
    })
  }
});


})
}

getMessagesNew(key){
  console.log('triggerd');
  this.setState({
      amountVideos: 0,
      downloadedVideos:0
  });

  this.messages = [];
  var orders = firebase.database().ref('messages/'+key+'/posts').orderByChild('post').limitToLast(3);
  orders.once('value', snapshot => { // observe i have changed "on" to "once"

  snapshot.forEach( (order) => {
      console.log('triggerd in loop')
      let state = order.val();
      if(state != null){
        var newelement = {date: state.date, function: state.function,image:state.image,name: state.name,  text: state.text, type:state.type, post: state.post,};
    if(newelement.type == false){
      this.setState({amountVideos: this.state.amountVideos+1})
      this.downloadImage(newelement);
    }else{
      messages.push(newelement);
      if(this.state.amountVideos == 0){
        this.setState({
          loading: false,
          messagesAvailable: true,
          refresh: this.state.refresh+1,
        })
      }else{
        console.log('videos laden');
        this.setState({
          refresh: this.state.refresh+1
        })
      }
       console.log('message local val'+this.messages);
       console.log('message state val'+this.state.messages);

    }
      
  }else{
    this.setState({
      messagesAvailable: false,
    })
  }
});


})