【问题标题】:Firebase Bind and Unbind from State in React when using Re-base使用 Re-base 时在 React 中从状态绑定和解除绑定
【发布时间】:2016-05-09 06:22:26
【问题描述】:

在将 re-base 模块与 Firebase 和 React 结合使用时,我在使用 React 以及通过应用程序从 Firebase 获取内容的方式方面遇到了一些问题。

我有两个组件/“页面”和

在家里,我只是“发表”评论,并显示刚刚发表的评论以及已发表多少 cmets 的计数器。在 cmets 中,我想显示所有已发布的 cmets。

我正在将 cmets 从 Home 添加到 firebase 及其工作...

var base = Rebase.createClass('https://my-app.firebaseio.com/');

componentDidMount : function() {

    base.bindToState('comments' , {

      context : this, 
      state : 'comments',

    });

这完全有效,我可以将 cmets “发布”到 firebase 并同步它们。当我从主页转到评论然后回来时,问题就出现了,我收到了这个错误。

Endpoint (comments) already has listener bindToState 

我试过这个:

componentWillUnmount: function() {

 base.removeBinding('comments');    


},

但是不工作。这难道不是在 React 中做这件事的正确方法吗,我是否遗漏了什么。我会很感激一些帮助。谢谢!

【问题讨论】:

    标签: javascript reactjs firebase synchronization state


    【解决方案1】:

    根据https://github.com/tylermcginnis/re-base 此处的文档,您的代码应该是...

    this.commentsRef = base.bindToState('comments' , {
       context : this, 
       state : 'comments',
    });
    

    还有……

    componentWillUnmount(){
      base.removeBinding(this.commentsRef);
    }
    

    【讨论】:

    • 非常感谢,我完全错过了它。相信我,我已经用了 3 天了 :( 我终于可以继续前进了.... :D
    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多