­2019-02-03 18:39:26
JavaScript
­, 2019-02-03 18:39:26

How to return a product back by changing the quantity?

There is such a wonderful example from the redux off site: CodeSandBox

By clicking on a product, its quantity becomes -1, and in the basket +1.
How to do the opposite? So that when you click on a product in the basket, it becomes -1, and in the general list +1.

Help please ✌️

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-02-03
@640

So , right?

M
Maxim, 2019-02-03
@maxfarseer

Create an action:

const removeFromCart = productId => ({
  type: types.REMOVE_FROM_CART,
  productId
})

then in the cart reducer, make sure that this action has its own case, and it returns a new array addedIds in which there will no longer be an id from your action if quantityById is less than one (that is, this position should no longer be in the cart) , or change the quantityById ... the point is that you return a new state.
also, for the product reducer, you make a case for the same action - and there you also find and add a new element by id.
In general, I think the point here is not so much in the implementation of the code, but in understanding what actions and reducers are. Read in the textbook (in Russian), do an example and I think you will understand your question without any problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question