简体   繁体   English

我是否通过替换对象中的整个数组来改变我的reducer状态?

[英]Am I mutating my reducer state by replacing an entire array in an object?

I have a shopping cart app. 我有一个购物车应用程序。 When I delete an item from the cart, I want to be able to add it back to the cart if the delete request fails. 当我从购物车中删除某个商品时,如果删除请求失败,我希望能够将其添加回购物车。

I do this by storing my previous array of items before deleting an item. 我通过在删除项目之前存储我之前的项目数组来完成此操作。 And if the request fails to delete the item, I replace back the entire array of items. 如果请求无法删除该项,我将替换整个项目数组。

Please note, there are multiple orders because the shopping cart can have multiple vendors, each with their own items to add to the cart. 请注意,有多个订单,因为购物车可以有多个供应商,每个供应商都有自己的项目添加到购物车。

Here's my reducer, that adds back the previous items to the cart if the delete an item request fails. 这是我的reducer,如果删除项目请求失败,则会将之前的项目添加回购物车。 items is an array that is a property of an order object items是一个数组,它是order对象的属性

My Reducer 我的减速机

    case 'ADD_PRODUCT_TO_CART_ERROR':

        return Object.assign({}, state, 
        {
            orders: 
                state.orders.slice(0, action.orderIndex)
                .concat([{
                    ...state.orders[action.orderIndex],
                    items: action.previousItems
                }])
                .concat(state.orders.slice(action.orderIndex + 1))                
        })

It works, but am I mutating any state here? 它有效,但我在这里改变任何州吗? Or does Object.assign() take care of that for me? 或者Object.assign()会为我处理这个问题吗?

as you are dealing your failed request to delete an item it's good that you are handling it in a completely different case 'ADD_PRODUCT_TO_CART_ERROR': . 当您处理failed request to delete an item ,您可以在完全不同的case 'ADD_PRODUCT_TO_CART_ERROR':处理它case 'ADD_PRODUCT_TO_CART_ERROR':

and here nothing is mutating as you have concating the previous array with state order it's ok to do that. 这里没有什么mutating ,你已经concating以前的array与国家秩序这是确定这样做。

indeed it's the right approach to handle such case. 事实上,这是处理此类案件的正确方法。

Object.assign creates a new object shallowly copying the properties from one (or many) objects into a new one. Object.assign创建一个新对象,将属性从一个(或多个)对象轻微复制到一个新对象中。

This means that if your state has nested objects they will be referenced from the new object (although it is a new object, if you remove a property from one it wont affect the other). 这意味着如果您的状态具有嵌套对象,则它们将从新对象引用(尽管它是一个新对象,如果从一个属性中删除属性,它不会影响另一个)。

If immutability is what you want, I recommend you use Immutable.js 如果你想要不变性,我建议你使用Immutable.js

The key requirement is that a reducer cannot modify an existing state object; 关键要求是reducer不能修改现有的状态对象; it must produce a new object. 它必须产生一个新的对象。 You can use functions that returns a new copy of data or if you don't want to take much work with those js immutable functions you can use the library immutable.js . 您可以使用返回新数据副本的函数,或者如果您不想对这些js不可变函数进行大量工作,则可以使用库immutable.js

And this question has some discussion on immutable.js 这个问题对immutable.js有一些讨论

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

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