尝试使用react编辑数据

时间:2016-05-20 05:05:59

标签: javascript reactjs

我对我要解决的问题感到非常困惑。我能够使用React在页面上呈现数据,但我希望能够在单击编辑按钮时更改值。单击按钮时,我正在提示用户输入新数据,我希望新数据替换页面上的旧数据。 editItem函数是我尝试执行此操作的位置。关于如何解决这个问题的任何建议都会非常有帮助。

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;

2 个答案:

答案 0 :(得分:0)

您可以利用本地状态和生命周期方法来实现这一目标。

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;

答案 1 :(得分:0)

您可以采取两种方法。

更新道具

您目前始终根据name值呈现this.props.name。如果您想要更新它,则必须在值更新时通知您的父组件,然后让父组件将新的prop值传递给子组件。

实施例

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);
 }

介绍州

第二种方法是将本地状态引入此组件。

实施例

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;