React组件DOM不更新

时间:2017-01-13 12:20:13

标签: javascript reactjs dom javascript-objects

我有一个对象数组。每个对象代表一个产品,它们被添加到该数组中并从中删除,DOM正确地反映了这一点。问题是当我更新这些对象的一个​​属性时,DOM没有反映这个更新。

import React from 'react'
import './App.css'

var stack = React.createClass ({
  getInitialState() {
    return {order: []};
  },
  AddToOrder(amount, product) {

    var order = this.state.order, isInOrder = false;

    order.map((object, i) => {
      if (object.productId === product.productId) {
        object.amount = product.amount + amount;  //This change doesn't show
        isInOrder = true;
      }
      return;
    })


    if (!isInOrder)  {
      product.amount = amount;
      order.push(product);        //This change is updated
    } 

    this.setState({order: order});

  },
  render() {
    return (
        <table>
          {<OrderRow products={this.state.order}/>}
        </table>    
      )
    }
})

class OrderRow extends React.Component {
  render() {
    return (
      <tbody>
        {this.props.products.map((object, i) => {
            return  <tr key={i}>
                <td> {object.name}</td>
                <td> 
                  <input     
                    type='number' defaultValue={object.amount} /> //This doesn't update in DOM when I change the products amount in stack component
                </td>

                </tr>;
              })}
          </tbody>
        );
      }
    }

export default stack;

在此示例中,&#34; ChangeName&#34;然而,函数更改反映在DOM中。这是为什么?

import React from 'react'
import './App.css'

var stack = React.createClass ({
  getInitialState() {
    return {order: []};
  },
  AddToOrder(amount, product) {

    var order = this.state.order;

    var isInOrder = false;

    order.map((object, i) => {
      if (order[i].productId === product.productId) {
        order[i].amount = product.amount + amount;  //This change doesn't show
        isInOrder = true;
      }
      return;
    })


    if (!isInOrder)  {
      product.amount = amount;
      order.push(product);        //This change is updated
    } 

    this.setState({order: order});
    return;

  },
  changeName() {
    var order = this.state.order;
    order[0].name = "name changed"; //Make sure there is one object in order
    this.setState({order: order});

  },
  render() {
    return (
        <table>
          {<OrderRow products={this.state.order}/>}
        </table>    
      )
    }
})

class OrderRow extends React.Component {
  render() {
    return (
      <tbody>
        {this.props.products.map((object, i) => {
            return  <tr key={i}>
                <td> {object.name}</td>
                <td> 
                  <input     
                    type='number' defaultValue={object.amount} /> 
                </td>
            </tr>;
          })}
      </tbody>
    );
  }
}

3 个答案:

答案 0 :(得分:1)

尝试此功能:

AddToOrder(amount, product) {

    var order = this.state.order, isInOrder=false;

    for(let i in order){
        if(order[i].productId === product.productId){       
            order[i].amount = product.amount + amount;
            isInOrder = true;
            break; // break it if table contains unique entries
        }
    }

    if (!isInOrder)  {
      product.amount = amount;
      order.push(product);      
    } 

    this.setState({order: order});
    return;
}

答案 1 :(得分:0)

您可以使用forceUpdate()

答案 2 :(得分:0)

在输入div中使用value代替defaultValue

<input type='number' defaultValue={object.amount} /> 

应该是

<input type='number' value={object.amount} /> 

如果有人知道原因,请编辑或单独回答。