使用React删除Firebase中的项,重新渲染会返回未定义的项
Removing item in Firebase with React, re-render returns item undefined
我正在构建一个学习React的应用程序,并使用Firebase作为我的数据存储。我有我的firebase渲染中的所有项目,并且正在尝试删除单个项目。然而,当我尝试删除时,点击删除按钮后,我得到了Uncaught TypeError: Cannot read property 'name' of null
,它指的是renderExpenditure
函数中的这行代码:
<strong>{details.name}</strong>, <strong>{h.formatPrice(details.amount)}</strong>, {details.category}, {details.type}, {details.date}
状态设置如下:
getInitialState: function() {
return {
cashbook: {
expenditure: {},
income: {}
},
totals: {},
available: {}
}
}
呈现项目并尝试删除它们的功能如下:
(有人能看到我做错了什么吗?还是这段代码太少,无法计算出发生了什么?)
提前谢谢。
在应用程序内
render: function() {
return(
<div className="cashbook">
<div className="expenditure">
<ul className="items">
{Object.keys(this.state.cashbook.expenditure).map(this.renderExpenditure)}
</ul>
</div>
</div>
);
}
renderExpenditure: function(key) {
var details = this.state.cashbook.expenditure[key];
return(
<li className="item" key={key}>
<strong>{details.name}</strong>, <strong>{h.formatPrice(details.amount)}</strong>, {details.category}, {details.type}, {details.date}
<button className="remove-item" onClick={this.removeExpenditure.bind(null, key)}>Remove</button>
</li>
);
},
removeExpenditure: function(key) {
this.state.cashbook.expenditure[key] = null;
this.setState({
expenditure: this.state.cashbook.expenditure
});
},
您在setState中设置了错误的值。支出不存在于根状态,因此必须覆盖包含它的父级。它应该是:
this.state.cashbook.expenditure[key] = null;
this.setState({
cashbook: this.state.cashbook
});
相关文章:
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 它将返回什么新日期(DateObject)
- 从映射返回React渲染数组
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- 在浅层渲染的反应组件上,Enzyme为道具返回null
- 返回一个新函数,而不是工厂中的对象
- Javascript映射对象成员以返回新的扁平对象
- javascript函数,它将数组作为输入,将每个数组元素增加1,并返回增加值的新数组
- 返回它在Javascript中构建的新对象
- 如何强制对模态进行新渲染
- 返回带有筛选结果的新JSON对象
- IE7 新函数返回对象而不是函数
- 如何使用javascript和jquery修改通用css,并将结果作为新id返回
- 关注标签触发模糊事件后新渲染的文本输入
- 如何正确渲染返回的AJAX POST MVC 4
- 新函数返回新函数
- JS字段渲染返回标准字段渲染
- 在Backbone.js中渲染返回获取的JSON数据
- WordPress REST API-新类别返回未定义,但仍然可以访问
- 在ReactJS中渲染/返回HTML5画布