ReactFire-获取'对象作为React子对象无效'将Firebase绑定到React状态时
ReactFire - get 'Objects are not valid as a React child' when binding Firebase to React state
我正试图用下面的教程代码将Firebase中的数据绑定到名为"notes"(数组)的反应状态:
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');
是否工作不正常?
firebase返回一个对象数组。当你做这个
{this.props.notes.map(function(note,index){
return <li key={index}>{note}</li>
})}
{注意}这是一个对象,我认为您需要通过对象键再次循环或使用https://www.npmjs.com/package/react-addons-create-fragment在您的应用程序中。
像这样的
var addons = require('react-addons-create-fragment');
return <li key={index}>{addons(note)}</li>
由于React的this.refs
,this.ref
有点令人困惑——而且,我不理解this.ref.child
。根据文档,这应该有效:
var ref = new Firebase('https://myname-github-notetaker.firebaseio.com'); //may need .com/something here
this.bindAsArray(ref, 'notes');
相关文章:
- 在react函数中渲染对象数组
- React-从简单对象树创建dom
- 在React中渲染子对象,丢失道具数据
- 访问要在 React Native Text 组件中显示的对象数组
- React.js+Flux-正确初始化存储中的数据对象
- React选择性地不将新道具传递给渲染对象
- TypeError:undefined不是对象(正在评估'this.props')-React Nati
- 如何从 React 中的事件对象访问自定义属性
- React是装载前处理空对象的最佳方式
- 从react routers browserHistory对象获取当前页面路径
- 测试使用 jQuery 和窗口对象的 React 组件
- JSON 对象 - 在 React Native 中访问值
- 在 React Native 中对对象数组进行动画处理
- 使用 Javascript/React.js 查找对象的数组索引
- 使用 React 创建的表单不会更新支持状态对象
- 在 react 中对字符串应用 span 会返回 [对象对象],而不是给我跨度文本
- 在 React 中从数组中删除对象
- 通过带有对象子级的数组进行 React 本机映射 - 工作方式与在 react web 中不同
- 如何通过 React 按其键返回对象内容(JSON 文件)
- 如何影响一个嵌套对象(React)中的一个键