Skip to content

Instantly share code, notes, and snippets.

@matiasfha
Created June 11, 2020 10:52
Show Gist options
  • Save matiasfha/4a0a22b4a905daf46e12a5bc54365e77 to your computer and use it in GitHub Desktop.
Save matiasfha/4a0a22b4a905daf46e12a5bc54365e77 to your computer and use it in GitHub Desktop.
Utilizar Object spread para asegurar inmutabilidad
// 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