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)
Here's what I understand so far:
Line 26: If case RECIEVE_PRODUCTS is not met, execute what comes after the double dots.
Line 27: const productId is set equal to action.productId carried over by the addToCart action.
Line 28: If action.productId exists, execute the statement between the curly brackets.
Line 29 & 30: Return the unchanged properties of state.
Line 31: ?
Line 32-34: Otherwise, return state unchanged.
[productId]: products(state[productId], action)
is assigning the result of products(state[productId], action)
to a key with the value of productId
. Its easier to explain with an example:
const a = 'foo'; const b = 'bar'; const obj = { [a]: a, b: b } console.log(obj);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.