【问题标题】:How to create flow data from firebase to react?如何从firebase创建流数据以做出反应?
【发布时间】:2017-09-24 14:21:49
【问题描述】:

我正在尝试连接 firebase 实时数据库以使用通量做出反应,我不包括只是为了缩短代码的操作,我的代码:

// in React parent component
componentDidMount() {
    // connecting to firebase when component completely mounted
    rootRef.child('nodes').on('value', snap => {
        // passing snap to Store and emiting changes
        Store.inject(snap)
    })
    Store.on('connect', this.listenDatabase)
}

listenDatabase() {
    this.setState({
        dataReceived: true,
        rootData: Store.getRootData()
    })
}


// in Store.js
inject(snap) {
    const data = snap.val();
    // do something with data
    // ...
    this.result = /* some data turned into array or object */
    this.emit('connect')
}

我的代码有效,但问题是这个流程是好的做法吗?

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database flux nosql


    【解决方案1】:

    你需要写getRootData函数

    // in Store.js
        inject(snap) {
        const data = snap.val();    // do something with data    // ...    this.result = /* some data turned into array or object */    this.emit('connect')
    
         getRootData(){
            return this.result
         }
    

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 2021-04-04
      • 2023-04-10
      • 2017-10-11
      • 1970-01-01
      • 2021-02-05
      • 2019-07-18
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多