Skip to content
Advertisement

TypeError: Cannot read property ‘qty’ of undefined. React Native Redux

I try to build an application called Cake Shop to sell and make Cakes, I have a problem reducing the number of cakes in my State, Here is my initial State

JavaScript

I think the problem is here in my Reducer, I want to reduce the quantity every time I dispatch the action.

JavaScript

This My Action

JavaScript

That’s how I call the action on my Screen.

JavaScript

Advertisement

Answer

Your current reducer line has a problem:

JavaScript

When this is run the first time, it sets cakes to a number — the quantity of the first item in the array – 1.

What you want to do instead is set cakes to a new array, with the quantity of that one item altered:

JavaScript

In a real-world example, it’s unlikely your action would ever just modify the first item of the array in a hard-coded way. The more likely scenario would be to contain an ID to alter in your action and then just update the item with a matching ID.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement