【问题标题】:dynamically querying Firestore in react native在本机反应中动态查询 Firestore
【发布时间】:2019-04-17 19:00:42
【问题描述】:

我正在从 firebase/fire 商店实时获取消息。但是我想通过将动态数字(日期)传递给 Firestore 中的 where 子句来动态查询数据,如果数据的数字(日期)大于给定的动态数字,则获取数据。我从 redux store 获取动态数据并通过 props 将其传递到 where。但问题是数字没有更新。我在构造函数中定义了 Firestore 的东西。我在下面附上了我的代码。 如果数据的日期大于给定日期,我的目标是从 Firestore 获取数据。

我尝试了很多方法,发现当我从后端发送消息时,redux 传递的动态日期没有更新,因为我在构造函数中使用了该道具值。

...
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) 


【问题讨论】:

    标签: react-native redux google-cloud-firestore react-native-firebase


    【解决方案1】:

    如果不创建新查询并重新订阅,您将无法更改 onSnapshot 查询。

    您可以利用componentDidUpdate lifecycle call 并检查 lastseen 属性的变化。

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

    虽然这可能会解决您的问题,但我想它会导致很多订阅操作,并重新渲染

    【讨论】:

    • 非常感谢。虽然我没有得到最终输出,但我在你的回答中得到了一些东西,“你不能在不创建新查询并重新订阅的情况下更改 onSnapshot 查询。”。你是对的,这种方式会导致很多重新渲染。所以它引发了另一个问题。所以我找到了另一种取消订阅和重新订阅的方法。我把所有东西都放在了 render 里面,效果很好。
    猜你喜欢
    • 2021-02-06
    • 2018-10-15
    • 2019-04-08
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多