【问题标题】:Set a value at the top level of a Firebase db with React使用 React 在 Firebase 数据库的顶层设置一个值
【发布时间】:2017-03-19 08:09:03
【问题描述】:

如何使用 React 在 Firebase 数据库的顶层设置值。

一个教程(在付费墙后面)解释了在子记录中设置一个值,但我还没有弄清楚如何在顶层进行。

更新子记录:

componentDidMount: function() {
  this.ref = new Firebase('<url>');
  var childRef = this.ref.child(this.props.params.username);
  this.bindAsArray(childRef, 'notes');
},
handleAddNote: function(newNote) {
  this.ref.child(this.props.params.username)
     .child(this.state.notes.length).set(newNote);
}...

尝试执行以下操作,但不确定如何获取顶层项目的长度。

componentDidMount: function() {
  this.ref = new Firebase('<url>');
  this.bindAsArray(this.ref, 'notes');
},
handleAddNote: function(newNote) {
  this.ref.child(this.state.notes.length).set(newNote);
}...

我几天前开始使用 React,因此我们非常感谢任何建议。

【问题讨论】:

    标签: javascript reactjs firebase-realtime-database


    【解决方案1】:

    解决方案:

    以前由于计算没有元素的特定状态的元素长度而无法正常工作,因此每次添加内容时都会覆盖 Firebase 数据库中的第一项(0 索引)。

    成功了:

    componentDidMount: function() {
      this.ref = new Firebase('<url>');
      this.bindAsArray(this.ref, 'notes');
    },
    handleAddNote: function(newNote) {
      this.ref.child(this.state.notes.length).set(newNote);
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多