score:3
Accepted answer
you open an object literal, start with ...state
to spread that out into the new object, and follow it with the cart
property (and the same concept for the inner one):
case increase_quantity: {
return {
...state,
cart: state.cart.map(item => {
if (item.product.id === action.productinfo.product.id) {
return {
...item,
quantity: action.quantity + 1,
};
}
return item;
}),
};
}
by doing it in that order, you ensure that cart
(or quantity
for the inner one) overrides the property from the spread. later properties in in object literals "win" over earlier ones.
score:1
object.assign({}, item, { quantity: action.quantity + 1 });
becomes
{ ...item, quantity: action.quantity + 1 }
case increase_quantity : {
return {
...state,
cart : state.cart.map(item => {
if (item.product.id === action.productinfo.product.id) {
return {
...item,
quantity: action.quantity + 1
}
}
return item;
})
};
}
Source: stackoverflow.com
Related Query
- Do I need to use the spread operator when using useState hook on object when updating?
- Convert object to use spread operator?
- How to use spread operator to delete an object from array of objects using spread operator using react?
- How to use spread operator on object create-react-app?
- why i need to use spread operator and make clone object to update object properties in react
- Need to know how to use the spread operator for an object
- How to use object spread with nested properties?
- Use Object.assign or Spread Operator in React/Redux? Which is a better practise
- Add object to the beginning of array using spread operator
- Update Multiple Properties of an object with spread operator
- React/Redux - Add object to start of array using spread operator
- How to push into an array of object using the spread operator to a specific element
- Update very deep object with spread operator
- how to modify a specific object at a index using spread operator in react-redux?
- Object assign and spread operator mutate React state?
- Is it possible to use spread out operator to set the react component props?
- Why not to use splice with spread operator to remove item from an array in react?
- What is right to use Spread operator or Object.assign in Redux?
- Why use the spread operator when calling 'setState()' in React?
- Why use the JavaScript spread operator to update an item in an array?
- How to use object spread reduxjs/toolkit
- Spread operator overwriting elements in new object instead of combining
- lodash , convert string from svg circle (properties and values separates with space) to object for spread in react component inline
- How do I use spread operator in useEffect without infinite loop?
- Change property in array with Spread Operator returns object instead of array
- How to use spread operator on nested javascript objects?
- Update array object object value using spread operator
- How do I use the spread operator to update a state value in React?
- Please explain the use of spread operator in this case
- Unable to use the spread operator after ejecting create-react-app
More Query from same tag
- Performance comparison between react hooks and react class
- auth/invalid-email The email address is badly formatted
- Builds a chat app in React, using axios and firestore
- React - Uncaught TypeError: Cannot read property 'func' of undefined
- React progress bar with each click
- How to don't trigger right mouse click on Ctrl+LeftMouseClick in Firefox on MacOS?
- Instead of html tag shows $$typeof : Symbol(react.element)?
- Converting Spotify Link (or any music streaming service link) to another streaming service link (ex. Apple Music, Amazon, Tidal, SoundCloud, Youtube)?
- Is there any way to map the <img > to every object in an array?
- How to get global index in devexpress datagrid with ReactJS
- How can I add a custom attribute to React's HTMLAttributes from a library?
- How to mock Publish/subscribe events on React Components when using PubSubJS with Jest/Enzyme?
- How to set variable in render with reactjs?
- How to remove overflow: hidden placed by bootstrap 5 modal?
- React Hook cannot be called inside a callback
- Why Spread syntax throwing error in nodejs
- React - Each child in an array or iterator should have a unique "key" prop
- Redux change state object property based on another changed parameter
- using grouped array in list component - react js
- Why one state effected to another state during changing in React-Redux?
- How to implement remote: true for react (rails)?
- While installing react it gets stuck in between
- React & Webpack & Docker: Inject env's at runtime in production to the docker-container
- If I deploy my react app, will the link to a localhost still be valid, or will I also need to host the localhost app?
- Props send after third click on element
- Blocking navigation in React App
- Redux Saga: Type 'unknown' is not assignable to type 'ServerResponse'
- React - setState not updating array
- different behavior with componentDidMount than useEffect when using jquery emoji plugin
- Preserve React component state with Relay.setVariables