Created
June 11, 2020 10:52
-
-
Save matiasfha/4a0a22b4a905daf46e12a5bc54365e77 to your computer and use it in GitHub Desktop.
Utilizar Object spread para asegurar inmutabilidad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Forma original Object.assign | |
const originalState = { foo: 'foo' } | |
const newState = Object.assign({}, originalState, { | |
visibilityFilter: action.filter | |
}) | |
//Forma con Spread | |
const newState = return { ...originalState, visibilityFilter: action.filter } | |
// Para composicion de objectos | |
// con Object.assign | |
// getAddedIds retorna un arreglo el que es mapeado para crear un arreglo de objetos en donde cada objeto tiene datos | |
// del producto basado en su id y su cantidad. | |
return getAddedIds(state.cart).map(id => | |
Object.assign({}, getProduct(state.products, id), { | |
quantity: getQuantity(state.cart, id) | |
}) | |
) | |
// con Spread | |
return getAddedIds(state.cart).map(id => ({ | |
...getProduct(state.products, id), | |
quantity: getQuantity(state.cart, id) | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment