如何从firebase创建流数据以做出反应?

时间:2017-04-27 00:38:34

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

我正在尝试连接firebase实时数据库以使用flux做出反应,我不包括仅用于缩短代码的操作,我的代码:

// 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')
}

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

1 个答案:

答案 0 :(得分:0)

你需要编写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
     }