ReactFire - 获取'对象无效作为React儿童'将Firebase绑定到React状态时

时间:2015-11-15 23:05:20

标签: javascript arrays object reactjs firebase

我正在尝试将Firebase中的数据绑定到名为' notes'(数组)的反应状态,其代码如下here所示:

var Profile = React.createClass({
  mixins: [Router.State, ReactFireMixin],

  getInitialState: function() {
    return {
      bio: [],
      repos: [],
      notes: []
    }
  },

  componentDidMount: function() {
    this.ref = new Firebase('https://myname-github-notetaker.firebaseio.com');
    var childRef = this.ref.child(this.getParams().username);
    this.bindAsArray(childRef, 'notes');
  },

...

});

我在控制台中收到此错误:

Uncaught Error:
Invariant Violation: Objects are not valid as a React child (found: object with keys {.value, .key}).
If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.

这是什么意思?第this.bindAsArray(childRef, 'notes');行无效吗?

2 个答案:

答案 0 :(得分:2)

firebase返回一个对象数组。 当你这样做时

{this.props.notes.map(function(note,index){
 return <li key={index}>{note}</li>
})}

{note}这是一个对象,我认为你需要再次通过对象键循环或在你的应用中使用https://www.npmjs.com/package/react-addons-create-fragment

像这样的东西

var addons = require('react-addons-create-fragment');
return <li key={index}>{addons(note)}</li>

答案 1 :(得分:0)

由于React this.ref

this.refs有点混乱 - 我也不理解this.ref.child。根据文档,这应该工作:

var ref = new Firebase('https://myname-github-notetaker.firebaseio.com'); //may need .com/something here
this.bindAsArray(ref, 'notes');