简体   繁体   English

尝试使用react编辑数据

[英]Trying to edit data using react

I am very confused about a problem I am trying to solve. 我对要解决的问题感到非常困惑。 I am able to render data on the page using React, but I want to be able to change the values when an edit button is clicked. 我可以使用React在页面上呈现数据,但是我希望能够在单击编辑按钮时更改值。 I am prompting the user for new data when the button is clicked and I want the new data to replace the old data on the page. 单击按钮时,我提示用户输入新数据,并且我希望新数据替换页面上的旧数据。 The editItem function is where I am attempting to do this. 我正在尝试执行editItem函数。 Any suggestions on how to solve this would be extremely helpful. 关于如何解决此问题的任何建议将非常有帮助。

const NewProduct = React.createClass({
  render: function() {
    return (
        <section>
           <div>Name: {this.props.name}</div>
           <div>Price: {this.props.price}</div>
           <div>Category: {this.props.category}</div>
           <button className="deleteButton" onClick={this.deleteItem}>Delete</button>
           <button className="editButton" onClick={this.editItem}>Edit</button>
         </section>
    );
  },

  deleteItem: function() {
        console.log(this.props.id);
        this.props.product.destroy();
    },

  editItem: function() {
    var name = prompt('What should the new name be?');
    <div>Name: {this.name.value}</div>
  }

});

export default NewProduct;

You can make use of the local state and life cycle method to achieve this. 您可以使用局部状态和生命周期方法来实现此目的。

const NewProduct = React.createClass({
      constructor(props) {
        super(props);
        const entity = {
            name: '',
            price : '',
            category: '',
         };
        this.state = {
          entity : entity
         };
       }

       componentWillReceiveProps(newProps){
         const entity = newProps.props;
         const entity = {
            name: entity.name,
            price : entity.price,
            category: entity.category,
         };
         this.setState({
           entity: entity
          });
        }

      render: function() {
        const entity = this.state.entity;
        return (
            <section>
               <div>Name: {entity.name}</div>
               <div>Price: {entity.price}</div>
               <div>Category: {entity.category}</div>
               <button className="deleteButton" onClick={this.deleteItem}>Delete</button>
               <button className="editButton" onClick={this.editItem}>Edit</button>
             </section>
        );
      },

      deleteItem: function() {
            console.log(this.props.id);
            this.props.product.destroy();
        },

      editItem: function() {
        var name = prompt('What should the new name be?');
        // here you need to just update the state based on the promt values or use a callback function passing the values and update the state.
      }

    });

    export default NewProduct;

There are two approaches you can take for this. 您可以采用两种方法。

Update props 更新道具

You are currently always rendering the name based on the this.props.name value. 您当前始终根据this.props.name值呈现name If you'd like to update this, you'd have to notify your parent component when the value should update, and then have the parent component pass the new prop value back down to the child. 如果您想更新此值,则必须在值应更新时通知您的父组件,然后让父组件将新的prop值传回给子对象。

Example

editItem: function() {
  var name = prompt('What should the new name be?');
  /* 
    handleNewName would be a function passed in as a prop from the parent component. 
    It will then be on the parent component to update the name, and pass in
    the updated name as a prop, which will trigger a re-render and update the
    value on the child NewProduct component.
  */
  this.props.handleNewName(name);
 }

Introduce state 介绍状态

The second way you can handle this is to introduce local state into this component. 处理此问题的第二种方法是将本地状态引入此组件。

Example

const NewProduct = React.createClass({
  getInitialState: function () {
     // Get initial value from props.
     return {
       name: this.props.name
     }
  },
  render: function() {
    return (
        <section>
           <div>Name: {this.state.name}</div>
           <div>Price: {this.props.price}</div>
           <div>Category: {this.props.category}</div>
           <button className="deleteButton" onClick={this.deleteItem}>Delete</button>
           <button className="editButton" onClick={this.editItem}>Edit</button>
         </section>
    );
  },

  deleteItem: function() {
     this.props.product.destroy();
  },

  editItem: function() {
    var name = prompt('What should the new name be?');
    this.setState({ name })
  }

});

export default NewProduct;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM