[英]How can I setup edit item functionality in this React application?
I have a React application I am building to learn React which adds Income, Expenditure and logs Transactions. 我正在构建一个React应用程序以学习React,它增加了收入,支出和日志交易。
I am struggling with how to go about setting up the ability to edit an entry. 我正在努力如何设置编辑条目的能力。
So I have a series of entries for each. 因此,我每个都有一系列条目。 The Expenditure entries React class is as follows:
支出条目React类如下:
const Expenditure = React.createClass({
renderExpenditure(key) {
const details = this.props.cashbook[key];
return(
<tr className="item" key={key}>
<td><strong>{details.name}</strong></td>
<td><strong>{h.formatPrice(details.amount)}</strong></td>
<td>{details.category}</td>
<td>{details.type}</td>
<td>{details.date}</td>
<td><button className="remove-item" onClick={this.props.removeCashflow.bind(null, key, 'expenditure')}>Remove</button></td>
</tr>
);
},
render() {
return(
<div className="expenditure">
<table id="exp-table">
<tbody>
{Object.keys(this.props.cashbook || {}).map(this.renderExpenditure)}
</tbody>
</table>
</div>
);
}
});
And removeCashflow, in the main App component, looks like: 在主App组件中,removeCashflow如下所示:
removeCashflow(key, type) {
this.state.cashbook[type][key] = null;
this.setState({
cashbook: { [type]: this.state.cashbook[type] }
});
},
Does anyone have any pointers or advise as to the best way to set up the ability to edit an entry in React? 是否有人有任何建议或建议以最佳方式设置在React中编辑条目的能力?
Unsure of where to start. 不确定从哪里开始。
Thank you :) 谢谢 :)
The first step would be to create a separate component for your entry, let's say Entry ;-) 第一步是为您的条目创建一个单独的组件,比如Entry ;-)
You would call your entry component with its props, and methods to be changed, like this : 您将使用其属性和要更改的方法调用入口组件,如下所示:
<Entry entry={this.props.cashbook[key]} key={key} id={key} onRemove={this.props.removeCashflow} onEdit={this.props.editEntry} />
The render function of your entry component would be similar to the content of your renderExpenditure function. 输入组件的render函数将类似于renderExpenditure函数的内容。 The button to remove would trigger a method called handleDelete this way:
要删除的按钮将以这种方式触发一个名为handleDelete的方法:
handleDelete() {
this.props.onDelete(this.props.id, 'expenditure')
}
And your component would also have a handleEdit method similar to this one: 而且您的组件也将具有与此类似的handleEdit方法:
handleEdit(someParams) {
var editEntry = this.props.entry
// perform the edit here
this.props.onEdit(this.props.id, editEntry)
}
The updated item would then be handled by your app component to replace the old one. 然后,您的应用程序组件将处理更新后的项,以替换旧项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.