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)