简体   繁体   English

React-Redux Reducers中Spread语法的目的

[英]Purpose of the Spread syntax in React-Redux Reducers

I'm trying to understand the purpose of the spread operator. 我试图了解点差运算符的目的。 From what I understand from the documentation, the spread syntax copies over the existing object and gets overridden when a new object is passed in. in the code below: 根据我从文档中了解的内容,传播语法会复制现有对象,并在传入新对象时被覆盖。在下面的代码中:

export default function reducer(state={
    user: [],
    fetching: false,
    fetched: false,
    error: null,
  }, action) {

    switch (action.type) {
      case "FETCH_USER_FULFILLED": {
        return {
          ...state,
          fetching: false,
          fetched: true,
          user: action.payload,
        }
      }
    }

    return state
}

So if my understanding is correct this means that '...state' returns the object: 因此,如果我的理解是正确的,则意味着'... state'返回该对象:

 {
    user: [],
    fetching: false,
    fetched: false,
    error: null,
  }

So if i substitute '...state' with the object i should get this: 因此,如果我用对象代替'... state',我应该得到这个:

 {
    user: [],
    fetching: false,
    fetched: false,
    error: null,
    fetching: false,
    fetched: false,
    user: user.payload
  }

Wouldn't it be redundant? 这不是多余的吗?

One of the core concepts of Redux is that you never mutate state . Redux的核心概念之一是您永远不会改变状态 When you start to mutate your state object, predictability and reliability of the App suffers. 当您开始更改状态对象时,应用程序的可预测性和可靠性会受到影响。

This is how Redux expects you to use reducers: 这是Redux期望您使用reducer的方式:

  • Every single time, you must return the new state object. 每一次,您都必须返回新的状态对象。
  • Even if you don't use a library like Immutable , you need to completely avoid mutation. 即使您不使用Immutable之类的库,也需要完全避免发生突变。

So it is all about the immutability guarantee . 因此,这与不变性保证有关 In order to achieve that we can use a library like Immutable or do it ourselves using Object.assign() . 为了实现这一点,我们可以使用Immutable之类的库,也可以使用Object.assign()

Object spread syntax is an elegant and clean alternative for Object.assign() . 对象传播语法Object.assign()一种优雅简洁的替代Object.assign()

Consider: 考虑:

var x = { a: 1, b: 2 };

now, 现在,

var y = Object.assign({}, x, { b: 3, c: 4 });

is equivalent to: 等效于:

var y = { ...x, b: 3, c: 4 };

either way y gets the value { a: 1, b: 3, c: 4 } . 任一种方式y都获得值{ a: 1, b: 3, c: 4 }


So in our reducer we may write: 因此,在我们的减速器中,我们可以编写:

return Object.assign({}, state, {
    product: Object.assign({}, state.product, {
        amount: action.calculated_price
    })
});

alternatively the spread way (which gives a better readable code): 或者,扩展方式 (这样可以提供更好的可读性):

return {
    ...state,
    product: {
        ...state.product,
        amount: action.calculated_price
    }
};

Read more here . 在这里阅读更多。

objects can have only one thing corresponding to one key so there can't be two user properties of an object. 对象只能具有一个键对应的一件事,因此对象不能有两个用户属性。 the idea of spread operator is that it spreads the object it is applied to. 散布算子的思想是散布要应用的对象。 For this example above same code you might not know all the properties of state and you want to change only some specific properties like fetching, fetched, and user. 对于上面相同代码的示例,您可能不知道状态的所有属性,并且只想更改某些特定属性,例如访存,访存和用户。

Using spread operator is same as using Object.assign 使用扩展运算符与使用Object.assign相同

Object.assign({}, state, {
      fetching: false,
      fetched: true,
      user: action.payload
})

but spread operator gives you a cleaner syntax to make an object which has some same properties and values with some other object 但是,散布运算符为您提供了一种更简洁的语法,以使对象与其他对象具有相同的属性和值

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

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