简体   繁体   English

我如何理解Redux购物车示例中的默认声明?

[英]How do I understand this default statement in the Redux shopping cart example?

Here's the statement: 声明如下:

default:
      const { productId } = action
      if (productId) {
        return {
          ...state,
          [productId]: products(state[productId], action)
        }
      }
      return state

Source: https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/shopping-cart (src/reducers/products/ - line 26) 来源: https : //codesandbox.io/s/github/reactjs/redux/tree/master/examples/shopping-cart (src / reducers / products /-第26行)

Here's what I understand so far: 到目前为止,这是我的理解:

Line 26: If case RECIEVE_PRODUCTS is not met, execute what comes after the double dots. 第26行:如果不满足大小写RECIEVE_PRODUCTS,请执行双点之后的内容。

Line 27: const productId is set equal to action.productId carried over by the addToCart action. 第27行:const productId设置为等于addToCart操作继承的action.productId。

Line 28: If action.productId exists, execute the statement between the curly brackets. 第28行:如果存在action.productId,请在大括号之间执行该语句。

Line 29 & 30: Return the unchanged properties of state. 第29和30行:返回状态的不变属性。

Line 31: ? 第31行:?

Line 32-34: Otherwise, return state unchanged. 第32-34行:否则,返回状态不变。

[productId]: products(state[productId], action) is assigning the result of products(state[productId], action) to a key with the value of productId . [productId]: products(state[productId], action)被分配的结果products(state[productId], action)与值的键productId Its easier to explain with an example: 用一个例子更容易解释:

 const a = 'foo'; const b = 'bar'; const obj = { [a]: a, b: b } console.log(obj); 

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

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