简体   繁体   English

如何在此React应用程序中设置编辑项目功能?

[英]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.

相关问题 如何在不阻止功能的情况下编辑此Javascript? - How can I edit this Javascript without blocking functionality? 单击删除按钮时删除了所有项目,以及如何使用React.js实现编辑/更新功能 - Deleted all item when click on delete button and how to implement the edit/update functionality using React.js 如何将 onClick 功能添加到 React Js 中的特定列? - How can I Add onClick functionality to particular column in React Js? 如何在此计时器中添加暂停功能? 反应母语 - How can i add a pause functionality in this timer ? React-Native 如何在我的 JSF web 应用程序中拥有 AJAX 功能? - How can I have AJAX functionality in my JSF web application? 在我的 React 应用程序中,如何在此映射函数中的每个项目之后添加换行符? - In my React application, how can I add a newline after each item in this map function? 如何在不更改整个列表的情况下编辑列表中的项目? - How can I edit an item in a list without changing the whole list? 我如何编辑Li项目以使用javascript向其添加文本 - How can i edit the Li item to add text to it with javascript React 中的多行编辑功能 - Multiple Row Edit Functionality in React 如何在本机反应中编辑平面列表项 - How to edit a flatlist item in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM