简体   繁体   English

更新ReactJS状态数组

[英]Update ReactJS state array

I have a ReactJS component that renders a list of orders. 我有一个ReactJS组件,可以呈现订单列表。 I get orders from a REST API. 我从REST API获得订单。 The format of the data is the following: 数据格式如下:

{
    "count": 2,
    "orders": [
        {
            "order_number": 55981,
            "customer_number": 24742
        },
        {
            "order_number": 55980,
            "customer_number": 24055
        }
    ]
}

Each order can have a list of items. 每个订单可以有一个项目列表。 When I click on an order, I get the list of items in the following format: 当我点击订单时,我得到以下格式的物品清单:

{
    "count": 2,
    "items": [
        {
            "name": "Green pillow",
            "status": "pending"
        },
        {
            "name": "Red pillow",
            "status": "delivered"
        }
    ]
}

The orders list is refreshed automatically and can change any time, so I store the orders list in this.state which gets updated via ajax. 订单列表会自动刷新,并且可以随时更改,因此我将订单列表存储在this.state ,该列表通过ajax进行更新。 this.state looks like this: this.state看起来像这样:

{
    "orders": [
        {
            "order_number": 55981,
            "customer_number": 24742
        },
        {
            "order_number": 55980,
            "customer_number": 24055
        }
    ]
}

My problem is that I would like that, when I click on an order, the state gets updated so that the clicked order contains the items associated to that order. 我的问题是,我想在单击订单时更新状态,以便所单击的订单包含与该订单关联的项目。 The order list would look like this after clicking on an item: 单击项目后,订单列表将如下所示:

{
    "count": 2,
    "orders": [
        {
            "order_number": 55981,
            "customer_number": 24742,
            "items": [
                {
                    "name": "Green pillow",
                    "status": "pending"
                }
            ]
        },
        {
            "order_number": 55980,
            "customer_number": 24055
        }
    ]
}

How can I add items to a specific order using this.setState()? 如何使用this.setState()将商品添加到特定订单? The problem is that setState seem to update data using keys, but my orders are in an array. 问题是setState似乎使用键更新数据,但是我的命令位于数组中。 I can probably copy the whole array and put the items key inside, but that seems overkill. 我可能可以复制整个数组,然后将项目关键字放入其中,但这似乎有些过分。 Am I taking the wrong approach? 我采用错误的方法吗?

I'm not entirely sure I got your question, but I think that what you're trying to achieve is add new orders to an array (push) which is located in your state. 我不完全确定我有您的问题,但我认为您要实现的目标是向位于您所在州的数组(推送)添加新订单。

If that's the case, you should something like this: 如果是这种情况,则应该这样:

// since you're orders it's not a plain array, you will have
// to deep clone it (e.g. with lodash)

let orders = _.clone(this.state.orders);
orders.push(newOrder);
this.setState(orders);

Why cloning the state before changing it is important? 为什么在更改状态之前先克隆状态很重要?

Immutability comes with some great properties (like easy equality comparison) and the React team is aiming towards that direction to improve performance more and more. 不变性具有一些出色的属性(例如,简单的相等性比较),React团队正朝着这个方向努力,以不断提高性能。

As of React 0.13 mutating state without calling this.setState will trigger a warning, and it will break entirely at some point in React's future (see the docs ) 从React 0.13开始,不调用this.setState突变state将触发警告,并且它将在React的未来完全中断(请参阅docs

Hope this helps 希望这可以帮助

Make a copy of the current state, modify the copy and use it as the new state. 制作当前状态的副本,修改副本并将其用作新状态。

This is just a simple example. 这只是一个简单的例子。 You might want to add some caching logic so you don't have to retrieve the order's item again and again when a user click on the same order multiple times. 您可能要添加一些缓存逻辑,这样当用户多次单击同一订单时,不必一次又一次地检索订单的项目。

var updatedOrder = _.clone(this.state.orders);

updatedOrder[0]["items"] = [ { "name": "Foo", "status":"bar" } ];

this.setState({
  orders: updatedOrder
});

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

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