-1

I am getting messages from firebase/fire store in realtime. But I want querying the data dynamically by passing a dynamic number(date) to where clauses in Firestore and getting data if the data's number(date) is greater than given dynamic number. I am getting the dynamic data from redux store and pass it to the where through props. but issue is the number is not updating. I defined the Firestore stuff inside the constructor. I have attached my code below. My goal is to get data from Firestore if the data's date is greater than given date.

I have tried many ways and find out that when I am sending messages from backend the dynamic date which pass through by redux is not updating bcoz I am using that props value inside my constructor.

...
import firebase from 'react-native-firebase'

import {addLastSeen} from '../Redux/Actions'
import {addMessage} from '../Redux/Actions'
import {connect} from 'react-redux'

firebase.initializeApp({
  apiKey: 'AIzaSyDA8acz_UcdHK1QaIPd6sG1Cp5bma_gTvg',
  projectId: 'notifaapp'
})

const firestore = firebase.firestore()

class Navigate extends React.Component{
    constructor(props) {
        super(props);
        this.ref = firestore.collection('messages')
                           .orderBy('date', 'desc')
                          .where("date",'>' ,this.props.lastSeen) 
                 // date is something like this 1555520642840
                 // this.props.lastSeen is getting from redux store via props
        this.unsubscribe = null;
    }
    componentDidMount() {
        this.unsubscribe = this.ref.onSnapshot(this.onCollectionUpdate)
    }
    componentWillUnmount() {
        this.unsubscribe();
    }
    onCollectionUpdate = (querySnapshot) => {
      const todos = [];
      querySnapshot.forEach((doc) => {
        const { title, complete, message, date } = doc.data();
        this.props.addLastSeen(date) // dispatching an action and update redux store
        todos.push({
          key: doc.id,
          title,
          message,
          date : new Date(date)
        });

      });
      alert(JSON.stringify(todos))
      todos.map(value => {
        message = {
            "key": value.key,
            "title": value.title,
            "body": value.message,
            "date": value.date,
            "read":"false",
            "archived":"false"
          }
          this.props.add(message)
      })
    }
    render(){
        return(
            ...
        )
    }
}

function mapStateToProps(state){
  return {
    lastSeen : state.lastSeen.date,// getting date from redux store
  }
}

export default connect(mapStateToProps, {add:addMessage, addLastSeen:addLastSeen})(Navigate) 


Uzama Zaid
  • 374
  • 3
  • 10

1 Answers1

0

You can't change the onSnapshot query without creating a new query and re-subscribing.

You can take advantage of the componentDidUpdate lifecycle call and check for a change in the lastseen property.

componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.lastSeen !== prevProps.lastSeen) {
    // un-subscribe and re-subsricbe with modified query
  }
}

Although this may solve your issue, I imagine it will cause a lot of subscription actions, and re-renders

ACzChef
  • 31
  • 3
  • Thank You so much. Though I did not get the final output, I got something in your answer that " You can't change the onSnapshot query without creating a new query and re-subscribing.". You are correct, this way cause lot of re-renders. So it trigger another problem. So I found another way to unsubscribe and resubscribe. I put every thing inside render, and it's worked fine. – Uzama Zaid Apr 18 '19 at 06:33